我想建造像,通用打印機與非類型模板參數
template<class Ostream, int N>
class StreamPrinter{
public:
StreamPrinter(Ostream& out, std::string tail = "\n", std::string head = "", std::string middle = "\t")
: out(out)
, tail(tail)
, head(head) {}
template<class... Data>
void operator()(const Data&... dat){
//if N = 3,
//out << head << dat1 << middle << dat2 << middle << dat3 << tail;
//where dat# means the #'s argument of dat...
}
private:
Ostream& out;
std::string tail;
std::string head;
std::string middle;
};
我想建造其行爲不同,這取決於模板參數N
operator()
。 上述代碼描述了N=3
的行爲。假設sizeof...(dat) >= N
。
我試了一段時間。但我未能實現。請給我一些建議。:)
如果'operator()'的參數個數與'N'不相同怎麼辦? – aschepler
參數個數可以大於'N'。剩下的參數將不會被打印。 – Sungmin