Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有非类型模板参数的通用打印机_C++_Templates_C++11 - Fatal编程技术网

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个参数,但可以在
操作符()中检查
通过一个
静态断言
。谢谢。我知道了。为了完整起见,你能在回答中添加静态断言部分吗?