简体   繁体   English

在 C++ 中实现类型列表

[英]implementing a typelist in C++

I'm studying metaprogramming in C++ with templates and I'm trying to implement a typelist, and operations on it.我正在使用模板研究 C++ 中的元编程,我正在尝试实现一个类型列表,并对其进行操作。

I'm defining a typelist as a variadic class template and operations as templated structs with partial specializations.我将类型列表定义为可变参数 class 模板,并将操作定义为具有部分专业化的模板结构。 Operations like Front, PopFront and PushFront work fine, but when I instantiate Back and Element (to index the typelist to get the n-th element), the compiler complains that I'm using an incomplete type: Front、PopFront 和 PushFront 之类的操作工作正常,但是当我实例化 Back 和 Element(以索引类型列表以获取第 n 个元素)时,编译器抱怨我使用的类型不完整:

/**** typelist ****/
template <typename... Types>
struct Typelist
{
};

/**** get first element ****/
template <typename List>
struct Front;

template <typename Head, typename... Tail>
struct Front<Typelist<Head,Tail...>>
{
    typedef Head type;
};

template <typename List>
using FrontT = typename Front<List>::type;

/**** pop first element ***/
template <typename List>
struct PopFront;

template <typename Head, typename... Tail>
struct PopFront<Typelist<Head,Tail...>>
{
    using type = Typelist<Tail...>;
};

template <typename List>
using PopFrontT = typename PopFront<List>::type;

/**** push first element ****/
template <typename List, typename Element>
struct PushFront;

template <typename... Elements, typename Element>
struct PushFront<Typelist<Elements...>,Element>
{
    using type = Typelist<Element,Elements...>;
};

template <typename List, typename Element>
using PushFrontT = typename PushFront<List,Element>::type;

/**** get last element ****/ 
template <typename List>
struct Back;

template <typename... Head, typename Tail>
struct Back<Typelist<Head...,Tail>>
{
    typedef Tail type;
};

/**** indexing ****/
template <typename List, unsigned Index>           // recursive case
struct Element
{
    using type = typename Element<typename PopFront<List>::type, Index - 1>::type;
};

template <typename List>
struct Element<List,0>                             // base case
{
    typedef typename Front<List>::type type;
};

// template <typename... Types>                        // base case
// struct Element<Typelist<Types...>,0>
// {
//     using type = typename Front<Typelist<Types...>>::type;
// };

// template <typename... Types, unsigned Index>        // recursive case
// struct Element<Typelist<Types...>,Index>
// {
//     using type = typename Element<typename PopFront<Typelist<Types...>>::type, Index - 1>::type;
// };

template <typename List, unsigned Index>
struct ElementI : ElementI<PopFrontT<List>,Index - 1>
{
};

template <typename List>
struct ElementI<List,0> : Front<List>
{
};

template <typename List, unsigned Index>
using ElementT = typename Element<List,Index>::type;

I understand that I can use template parameter packs for any parameter in partial specializations as long as arguments can be deduced, so I think the declaration is correct, right?我知道只要可以推导出 arguments,我可以将模板参数包用于偏特化中的任何参数,所以我认为声明是正确的,对吧?

EDIT Element now works, I made a spelling mistake calling it, Back still doesn't, and I can't understand why. EDIT Element 现在可以工作了,我在调用它时犯了一个拼写错误,Back 仍然没有,我不明白为什么。

EDIT this is the code to test the typelist and the compiler (GCC 7.2) error (I slightly changed the implementation of typelist):编辑这是测试类型列表和编译器(GCC 7.2)错误的代码(我稍微改变了类型列表的实现):

EDIT EDIT compiler is GCC 7.2编辑编辑编译器是 GCC 7.2

#include "typelist.hpp"
#include <type_traits>

int main(int argc, char **argv)
{
    Typelist<int, double, bool> tl;

    static_assert(std::is_same<typename Front<decltype(tl)>::type,int>::value, "not same");
    static_assert(std::is_same<typename PopFront<decltype(tl)>::type,Typelist<double,bool>>::value, "not same");
    static_assert(std::is_same<typename PushFront<decltype(tl),float>::type,Typelist<float,int,double,bool>>::value, "not same");
    /* compiler error */ static_assert(std::is_same<typename Back<decltype(tl)>::type,bool>::value, "not same");
    static_assert(std::is_same<typename ElementI<decltype(tl),0>::type,int>::value, "not same");
    static_assert(std::is_same<typename ElementI<decltype(tl),1>::type,double>::value, "not same");
    static_assert(std::is_same<ElementT<decltype(tl),2>,bool>::value, "not same");

    return 0;
}


main.cpp: In function 'int main(int, char**)':
main.cpp:11:61: error: invalid use of incomplete type 'struct Back<Typelist<int, double, bool> >'
     static_assert(std::is_same<typename Back<decltype(tl)>::type,bool>::value, "not same");
                                                             ^~~~
In file included from main.cpp:1:0:
typelist.hpp:48:8: note: declaration of 'struct Back<Typelist<int, double, bool> >'
 struct Back;
        ^~~~
main.cpp:11:70: error: template argument 1 is invalid
     static_assert(std::is_same<typename Back<decltype(tl)>::type,bool>::value, "not same");

With clang++ (Apple clang version 11.0.3 (clang-1103.0.32.59)) I get the following message:使用clang++ (Apple clang 版本 11.0.3(clang-1103.0.32.59))我收到以下消息:

t.cpp:51:8: error: class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct Back<Typelist<Head...,Tail>>
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp:50:23: note: non-deducible template parameter 'Head'
template <typename... Head, typename Tail>
                      ^
t.cpp:50:38: note: non-deducible template parameter 'Tail'
template <typename... Head, typename Tail>
                                     ^

From: https://en.cppreference.com/w/cpp/language/parameter_pack (sorry i don't have a primary ref)来自: https://en.cppreference.com/w/cpp/language/parameter_pack (对不起,我没有主要参考)

In a primary class template, the template parameter pack must be the final parameter in the template parameter list.在主 class 模板中,模板参数包必须是模板参数列表中的最后一个参数。
In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments, or have default arguments: In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments, or have default arguments:

template<typename... Ts, typename U> struct Invalid; // Error: Ts.. not at the end

template<typename ...Ts, typename U, typename=void>
void valid(U, Ts...);     // OK: can deduce U
// void valid(Ts..., U);  // Can't be used: Ts... is a non-deduced context in this position

valid(1.0, 1, 2, 3);      // OK: deduces U as double, Ts as {int,int,int}

But we can solve the issue by defining Back in terms of Element .但是我们可以通过根据Element定义Back来解决这个问题。

/**** get last element ****/
template <typename List>
struct Back;

template <typename... Args>
struct Back<Typelist<Args...>>
{
    using type = typename Element<Typelist<Args...>, sizeof...(Args) - 1>::type;
};

Question:问题:

Why do you have template using statements for all your types except Back and Element ?为什么除了BackElement之外的所有类型都有template using语句?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM