C++ 模板元编程:如何创建和迭代“typedefs"类型列表在模板类中. [英] c++ template metaprogramming: how to create and iterate over list of types that are "typedefs" in template class.
问题描述
我的代码是一个工厂,它根据模板参数的类型创建对象.我想将其扩展为列表"类型.
My code is a factory that creates objects based on types from template parameters. I want to extend this to a "list of" types.
这就是我所拥有的:Algo1
定义了一个类型 indata
.FASSubscriberFactory::Create()
返回一个指向 FASSubscriber
的指针.请看这里:
This is what I have: Algo1
defines a type indata
. FASSubscriberFactory::Create()
returns a pointer to FASSubscriber<Algo1::indata,..>
. See here:
struct Algo1
{
typedef DataType1 indata;
}
template <class T, class FEED = T::indata, class PROC = typename ProcessorFactory<T>::ptype>
struct FASSubscriberFactory
{
typedef FASSubscriber<typename PROC , typename FEED > fftype;
static fftype * Create()
{
return new fftype(FASConfig::Data2Feed<FEED>::name, ProcessorFactory<T>::Create());
}
}
void main()
{
auto myFASSubscriber4Algo1 FASSubscriberFactory<Algo1>::Create();
}
这就是我想要的:Algo1
定义了一个 typedef 列表 indata
.FASSubscriberFactory::CreateList()
返回指向 FASSubscriber
列表的指针em>foreach 输入 Algo1:indata
.参见下面伪代码中的//注释.
This is what I want: Algo1
defines a list of typedefs indata
. FASSubscriberFactory::CreateList()
returns a pointer to a list of FASSubscriber<Algo1::indata,..>
foreach type in Algo1:indata
. See //comments in pseudocode below.
struct Algo1
{
//Want to define a list of types
typedef std::list<types> indata = { DataType1, DateType2 }
}
template <class T, class FEEDs = T::indata, class PROC = typename ProcessorFactory<T>::ptype>
struct FASSubscriberFactory
{
//want to create a list FASSubscribers from list of types T::indata
typedef list<FASSubscriber<PROC, FEEDs::type> listoffftypes
static lisoftypes * CreateList()
{
listoffftypes mylot();
//for each type in FEEDs - want to lopp around list of types
foreach(feedtype in FEEDs )
{
mylot.push(Create<feedtype>());
}
return mylot;
}
template <class FEED>
static fftype * Create()
{
typedef FASSubscriber<typename PROC , typename FEED > fftype;
return new fftype(FASConfig::Data2Feed<FEED>::name, ProcessorFactory<T>::Create());
}
}
void main()
{
auto myListOfFASSubscriber4Algo1 FASSubscriberFactory<Algo1>::Create();
}
我真正想要的是一种定义和迭代模板参数类中定义的类型列表"的方法.看了一下 A. Alexa 的 TYPELISTS,但我没有看到任何循环.
All I really want is a way to define and iterate over a "typelist" which is defined in the template argument class. Took a look at A. Alexa's TYPELISTS, but I didnt see any loops.
谢谢j
推荐答案
我有一种感觉 C++11 中的可变参数模板和 std::tuple
是你想要的,虽然我不是完全确定我明白你在问什么.
I have a feeling variadic templates and std::tuple
from C++11 is what you want, although I'm not exactly sure I understand what you're asking.
// returns a tuple of pointers created by a Create for each type in the parameter pack
template<typename... TS>
static std::tuple<TS*...> CreateList() {
return { Create<TS>()... };
}
请不要用普通的 C++ 代码来描述模板元编程;令人困惑.
Please don't describe template metapogramming in terms of normal C++ code; it is confusing.
例如,如果你这样称呼它:
For instance if you called it like this:
FASSubscriberFactory</* stuff */>::CreateList<int, float, foo, bar>()
它本质上是这样做的:
static std::tuple<int*, float*, foo*, bar*> CreateList() {
return { Create<int>(), Create<float>(), Create<foo>(), Create<bar>() };
}
这篇关于C++ 模板元编程:如何创建和迭代“typedefs"类型列表在模板类中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!