Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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+中打印编译时整数序列+;11_C++_C++11_Sequence_Variadic Templates_Variadic - Fatal编程技术网

C++ 在C+中打印编译时整数序列+;11

C++ 在C+中打印编译时整数序列+;11,c++,c++11,sequence,variadic-templates,variadic,C++,C++11,Sequence,Variadic Templates,Variadic,因此,我正在做一些家庭作业,我必须用C++11编写自己的编译时整数序列,并为它编写一些函数(print、concat、sort等),但我在如何编写这些东西的问题上遇到了一些麻烦 template<typename T, typename Comp = std::less<int>> struct Facility{ template<T ... Nums> struct List{ struct Element<T .

因此,我正在做一些家庭作业,我必须用C++11编写自己的编译时整数序列,并为它编写一些函数(print、concat、sort等),但我在如何编写这些东西的问题上遇到了一些麻烦

template<typename T, typename Comp = std::less<int>>
struct Facility{

    template<T ... Nums>
    struct List{

        struct Element<T ... nums>{};

        template<unsigned num, T val, T ... rest>
        struct Element{
            unsigned index = num;
            T value = val;
            Element<index-1, rest...> others;
        };

        template<unsigned num, T val, T ... rest>
        struct Element<0, val>{
            unsigned index = 0;
            T value = val;
        };

        static constexpr Element<sizeof...(Nums)-1,Nums...> elem = {};

        static void Print()
        {
            // Prints out the list
        }
    };

};

using IntList = typename Facility<int>::List<intlist...>;

int main()
{
    using List1 = IntList<1, 2, 3>;
    List1::print()
}
模板
结构设施{
模板
结构列表{
结构元素{};
模板
结构元素{
无符号索引=num;
T值=val;
其他要素;
};
模板
结构元素{
无符号索引=0;
T值=val;
};
静态constexpr元素elem={};
静态无效打印()
{
//打印出清单
}
};
};
使用IntList=typename Facility::List;
int main()
{
使用List1=IntList;
列表1::print()
}

我只是想知道我是否在正确的轨道上,这样我就不会让自己陷入死胡同。我对
列表中的
static print()
static constexpr
成员没有100%的把握,尽管我想不出任何其他方法来实现它。

不要不必要地嵌套这样的类型。写一个
序列

不要将操作与类型耦合。外部写入操作(尾部、头部)

从C++14中的
std::integer_序列
中获得灵感

如果你需要一个你在OP中描述的接口,那么就用一个平面的接口来代替

到目前为止,最容易编写的排序是合并排序

利用
std::integral_常量
,即C++11。编写一个元函数,它接受一个模板参数和一个整数列表,并将每个整数常量作为一个类型传递给,并生成一个类型列表
templatestruct types{}作为输出。将此调用为foreach\u int


编写foreach_type,它接受一个类型列表并调用每个元素上的函数对象。现在印刷品是微不足道的
template void print_list(){foreach_type(foreach_int{},print{})}

不要将操作与类型耦合。从外部写入操作(尾部、头部)

从C++14中的
std::integer_序列
中获得灵感

如果你需要一个你在OP中描述的接口,那么就用一个平面的接口来代替

到目前为止,最容易编写的排序是合并排序

利用C++11的
std::integral_constant
。编写一个元函数,它接受模板参数和一个整数列表,并将每个整数常量作为一个类型传递给,并生成一个类型列表
templatestruct types{};
作为输出。调用此
foreach_int


编写foreach_type,它接受一个类型列表并在每个元素上调用一个函数对象。现在print很简单;
template void print_list(){foreach_type(foreach_int{},print{})}
其中
template void print(T&&T}{std::cout我不清楚您到底想要获得什么以及您所做的事情的意义(为什么
设施
?为什么
列出设施内的

我只是举一个例子,说明如何使用未使用的数组编写
Print()
,而不使用递归(并根据雅克的建议定义
IntList
,灵感来自
std::integer\u sequence

关于concat和sort,我想您希望成员函数返回一个
IntList
,并将两个数字列表串联起来

一个简单的concat示例可以是
IntList

template <T ... Nums2>
static constexpr IntList<T, Nums..., Nums2...>
   Concat (IntList<T, Nums2...> const &)
 { return {}; } 
模板
静态constexpr IntList
Concat(内部列表常量&)
{返回{};}
所以你可以写一些像

constexpr IntList<int, 1, 2, 3> l1;
constexpr IntList<int, 4, 5, 6> l2;

constexpr auto l3 = l1.Concat(l2);

l3.Print(); // print 1, 2, 3, 4, 5, 6,
constexpr IntList l1;
constexpr IntList l2;
constexpr auto l3=l1.Concat(l2);
打印();//打印1,2,3,4,5,6,

我把排序函数留给您作为一个简单的练习:-)

我不清楚您到底想要获得什么,以及您所做的工作的意义(为什么
设施
?为什么
列表
设施内部?)

我只是举一个例子,说明如何使用未使用的数组编写
Print()
,而不使用递归(并根据雅克的建议定义
IntList
,灵感来自
std::integer\u sequence

关于concat和sort,我想您希望成员函数返回一个
IntList
,并将两个数字列表串联起来

一个简单的concat示例可以是
IntList

template <T ... Nums2>
static constexpr IntList<T, Nums..., Nums2...>
   Concat (IntList<T, Nums2...> const &)
 { return {}; } 
模板
静态constexpr IntList
Concat(内部列表常量&)
{返回{};}
所以你可以写一些像

constexpr IntList<int, 1, 2, 3> l1;
constexpr IntList<int, 4, 5, 6> l2;

constexpr auto l3 = l1.Concat(l2);

l3.Print(); // print 1, 2, 3, 4, 5, 6,
constexpr IntList l1;
constexpr IntList l2;
constexpr auto l3=l1.Concat(l2);
打印();//打印1,2,3,4,5,6,

我将排序函数留给您作为简单的练习:-)

抱歉,说实话,我很难理解其中的大部分。当你说不必要的嵌套类型时,你是指设施和列表吗?如果是,它们都是作为分配代码的一部分提供的。你说将操作耦合到类型是什么意思?澄清一下,用平面表示,你是指非递归的吗?比如t中的链接继承uples?抱歉,说实话,我很难理解其中的大部分。当你说不必要的嵌套类型时,你是指Facility&List吗?如果是的话,它们都是作为赋值代码的一部分提供的。你说将操作耦合到类型是什么意思?澄清一下,用flat,你是指非递归的吗?比如在元组?任务大纲中提供了设施和列表。设施中提供了排序等功能,而打印是唯一的功能