我想在编译时通过逐个添加类型来组装一个列表(实际上是一组).像这样的东西:
struct HeadOfList;
struct Item1;
[ addToList<Item1, HeadOfList> ]
struct Item2;
[ addToList<Item2, HeadOfList> ]
Run Code Online (Sandbox Code Playgroud)
我不在乎列表的存储方式.我想到这样的事情:
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; };
Run Code Online (Sandbox Code Playgroud)
但是a 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) >());
}
}
Run Code Online (Sandbox Code Playgroud)
可以在此处或此处找到更详细的示例,该示例还包含用于简化列表使用的迭代器.
"图书馆"最重要的部分:
/// 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>() ))
Run Code Online (Sandbox Code Playgroud)
它使用NextInListHelper函数声明和Argument Dependent(Name)Look-up来记录列表.可以使用Back宏引用列表的最后一个元素.
列表的第一个元素可以被访问为:Next(ListId, CTList::Nil),列表的最后一个元素可以被识别,因为它与下一个元素(LastItem == Next(ListId, LastItem))相同.
我只使用gcc 4.6.3进行了测试,但它的目的是完全符合C++ 11标准.
关于解决方案的几句话:
Back()宏添加额外的行来扩展ListId使用的附加类型NextInListHelper是允许类型同时包含在多个列表中Wrapper 用于避免列表元素类型的实际实例化,并支持不完整类型作为ListId PushBack()'calls'必须位于ListId(NS1)或Wrapper(全局范围)的命名空间中NS2)