我想构建类似的东西,
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;
};
我想构建operator()
,其行为会有所不同,具体取决于模板参数N
。
N=3
的行为在上面的代码中描述。假设sizeof...(dat) >= N
。
我试了一段时间。但我没能实现它。请给我一些建议。:)
答案 0 :(得分:3)
#include <string>
#include <iostream>
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)
{
static_assert(sizeof...(Data) >= N,
"Not enough arguments supplied for `operator()`");
out << head;
print(N, dat...);
out << tail;
}
private:
void print(int) {}
template<class D, class... Data>
void print(int printed, D const& p, Data const&... pp)
{
if(0 == printed) { return; }
out << p;
if(sizeof...(Data) > 0)
{
out << middle;
print(printed-1, pp...);
}
}
Ostream& out;
std::string tail;
std::string head;
std::string middle;
};
int main()
{
StreamPrinter<std::ostream, 4> foo(std::cout);
foo(1,2,3,4,5,6);
}
答案 1 :(得分:1)
您可以将其委托给专门针对N
template<int N>
struct StreamHelper {
template<
typename OS,
class... Data,
class = typename std::enable_if< std::is_same< std::integral_constant<int,N>, std::integral_constant<int,sizeof...(Data)> >::value >::type >
void apply(
OS& os,
const std::string& head,
const std::string& mid,
const std::string& tail,
const Data&... dat)
{
os << head;
apply_impl(os, mid, tail, dat);
}
private:
template<typename OS>
void apply_impl(
OS& os,
const std::string& mid,
const std::string& tail)
{
os << tail;
}
template<typename OS, class D0, class... D>
void apply_impl(
OS& os,
const std::string& mid,
const std::string& tail,
const D0& d0,
const D&... d)
{
os << d0 << mid;
apply_impl(os, mid, tail, d);
}
};
<强>更新强>
我更新了我的代码,以说明如何通常地完成它。