c++ - 逐一组装类型的编译时列表 (C++)

标签 c++

我想在编译时通过一个一个地添加类型来组装一个类型列表(实际上:一组)。像这样:

struct HeadOfList;

struct Item1;
[ addToList<Item1, HeadOfList> ]

struct Item2;
[ addToList<Item2, HeadOfList> ]

我不关心列表是如何存储的。我想到了这样的事情:

template<typename T> struct NextInList { typedef void type; };
template<> struct NextInList<HeadOfList> { typedef Item1 type; };
template<> struct NextInList<Item1> { typedef Item2 type; };
template<> struct NextInList<Item2> { typedef Item3 type; };

但是一个boost::mpl::list一样好。类型的顺序也无关紧要,我只希望能够遍历它们并向它们添加新元素。

我对此有一种不好的预感,因为这样的结构意味着例如LastElementOf<MyList>::type会在源文件的不同点(添加新元素之前和之后)编译为不同类型,这对我来说似乎是假的。尽管如此,这正是我现在想要的。

你觉得有可能吗?允许使用 C++11。

更新:我只想补充一点,我不知道在添加新元素时添加到列表中的最后一个元素,但我知道列表本身(例如列表)

最佳答案

在咨询了 comp.lang.c++.moderated 之后,我想出了下面详述的代码。 PushBack() 宏用于向列表中添加新项目,可以使用 Next() 宏遍历项目,最后一项可以被识别从它的“下一项”就是它自己。所以一个非常基本的示例代码:

struct A {}; 

namespace NS1 { 
    struct B {}; 
    struct C {}; 
    struct D {}; 

    // building up the compile-time list 
    struct Head; 

    PushBack(Head, A); 
    PushBack(Head, B); 
} 

PushBack(NS1::Head, NS1::C); 

namespace NS1 { 
    PushBack(Head, D); 
} 



// iterate through the list
namespace NS2 { 

    // end of the list reached (recognized from PrevItem == Item)
    template <class ListId, class Item> 
    void print(Wrapper<Item>, Wrapper<Item>) {} 

    // process Item and advance to the next element
    template <class ListId, class PrevItem, class Item> 
    void print(Wrapper<PrevItem>, Wrapper<Item>) 
    { 
        cout << typeid(Item).name() << endl; 
        print<ListId>(Wrapper<Item>(), Wrapper< Next(ListId, Item) >()); 
    } 
} 

可以找到一个更详细的示例,其中还包含一个迭代器以简化列表的使用 herehere .

“图书馆”最重要的部分:

/// Helper class to wrap incomplete types and avoid instantiation of T 
template<class T> struct Wrapper {}; 

namespace CTList { 
    /// The front of compile-time lists 
    struct Nil {}; 
} 

/// Compile-time list helper 
template< typename ListId, typename Item > 
Item NextInListHelper( ::Wrapper<ListId>, ::Wrapper<Item> ); 

/// The last element of the list
#define Back(ListId) \ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
    decltype( NextInListHelper( ::Wrapper<ListId>(), ::Wrapper<\ 
                             CTList::Nil \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) \ 
     >())) 



/// Add a new element (type) to the list 
#define PushBack( ListId, item) \ 
    item NextInListHelper( ::Wrapper< ListId >, ::Wrapper< Back(ListId) > ) 

/// The next element in the ('ListId') list after 'item' 
#define Next(ListId, item) decltype(NextInListHelper(::Wrapper<ListId>(), ::Wrapper<item>() )) 

它使用 NextInListHelper 函数声明和 Argument Dependent (Name)Look-up 来记录列表。可以使用 Back 宏引用列表的最后一个元素。

列表的第一个元素可以通过以下方式访问:Next(ListId, CTList::Nil), 列表的最后一个元素可以从它是 与其下一个元素相同 (LastItem == Next(ListId, LastItem))。

我只用 gcc 4.6.3 测试过它,但它旨在完全兼容 C++11。

关于解决方案的几句话:

  • 在这种形式下,它可以处理最多 10 个元素的类型列表,但这可以通过向 Back() 宏添加额外的行来扩展
  • “列表”只能包含一次类型
  • NextInListHelper 声明中使用的附加 ListId 类型允许类型同时包含在多个列表中
  • Wrapper 用于避免列表元素类型的实际实例化,并支持不完整的类型如 ListId
  • PushBack() 'calls' 必须在 ListId (NS1) 或 Wrapper (全局范围)
  • 列表的处理可以放在任意命名空间(NS2)

关于c++ - 逐一组装类型的编译时列表 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910584/

相关文章:

c++ - 访问 range_expression 内的嵌套元素返回不完整的映射(段错误)

android - 尝试为 Android 构建 Xerces-C++

c++ - MQL4 到 Microsoft Visual Studio 2017 社区上的 C++ dll

c++ - 多尺度检测循环在 OpenCV 的 HOG 检测中是如何工作的?

c++ - gcc 4.2 编译器 (Mac OSX) : fpu_control. h: 没有那个文件或目录的新手问题

c++ - C++ 中的自由定理 : are templates inherently ignorant and neutral with their objects of unknown types?

c++ - 使用成员访问运算符 (.) 初始化结构中的 char 数组

C++验证驾照程序

C++ unordered_set vector

c++ - 两次调用 EnumServicesStatusEx() 时,我仍然在 C++ 中得到 EROR_MORE_DATA