c ++模板元编程:如何创建和迭代"typedef"类型的列表.在模板类中. [英] 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< Algo1 :: indata,..>
的指针.看到这里:
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
定义typedefs indata
的列表. FASSubscriberFactory :: CreateList()
返回指向 FASSubscriber< Algo1 :: indata,..>
列表的指针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 ++模板元编程:如何创建和迭代"typedef"类型的列表.在模板类中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!