C++ 具有非类型模板参数的通用打印机
我想建造一些类似的东西C++ 具有非类型模板参数的通用打印机,c++,templates,c++11,C++,Templates,C++11,我想建造一些类似的东西 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) {} tem
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;
};
模板
类流式打印机{
公众:
StreamPrinter(Ostream&out,std::string tail=“\n”,std::string head=“”,std::string middle=“\t”)
:out(out)
,尾(尾)
,头(头){}
模板
void运算符()(常量数据和…数据){
//如果N=3,
//out您可以将其委托给在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);
}
};
模板
StreamHelper结构{
模板<
typename操作系统,
类…数据,
class=typename std::enable_如果::value>::type>
无效申请(
操作系统和操作系统,
常量标准::字符串和头,
常量标准::字符串和中间,
常量标准::字符串和尾部,
常量数据(&…dat)
{
os您可以将其委托给在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);
}
};
模板
StreamHelper结构{
模板<
typename操作系统,
类…数据,
class=typename std::enable_如果::value>::type>
无效申请(
操作系统和操作系统,
常量标准::字符串和头,
常量标准::字符串和中间,
常量标准::字符串和尾部,
常量数据(&…dat)
{
操作系统#包括
#包括
模板
类流式打印机{
公众:
StreamPrinter(Ostream&out,
std::string tail=“\n”,
std::字符串头=”,
std::string middle=“\t”)
:out(out)
,尾(尾)
,头(头){}
模板
void运算符()(常量数据和…数据)
{
静态断言(sizeof…(数据)>=N,
“没有为`operator()``提供足够的参数”;
out#包括
#包括
模板
类流式打印机{
公众:
StreamPrinter(Ostream&out,
std::string tail=“\n”,
std::字符串头=”,
std::string middle=“\t”)
:out(out)
,尾(尾)
,头(头){}
模板
void运算符()(常量数据和…数据)
{
静态断言(sizeof…(数据)>=N,
“没有为`operator()``提供足够的参数”;
如果operator()
的参数数与N
的参数数不相同怎么办?参数数可以大于N
。其余参数将不会打印。如果operator()的参数数
与N
不一样?参数的数量可以大于N
。剩余的参数将不会被打印。那么,是否有一种通用的方法来实现我上面描述的功能,而不专门针对某些N
?那么,是否有一种通用的方法来实现我上面描述的功能,而不需要spe为一些N
进行广告化?太好了。谢谢:)@Sungmin它不需要>=N个参数,但可以在操作符()中检查
通过一个static\u assert
。谢谢。我知道了。你能在你的答案中添加static\u assert部分以保证完整性吗?太好了。谢谢:)@Sungmin它不需要>=N个参数,但可以在操作符()中检查
通过一个静态断言
。谢谢。我知道了。为了完整起见,你能在回答中添加静态断言部分吗?