简体   繁体   English

C ++模板元编程 - 根据运行时输入返回一个类型

[英]C++ template metaprogramming - return a type based on runtime input

I am trying to create a templatized vector, where the template type is determined by runtime parameters. 我正在尝试创建一个模板化的向量,其中模板类型由运行时参数确定。 I am aware that template stuff is completed before runtime, basically I am trying to funnel runtime input into a select few previously selected compile-time types. 我知道模板的东西是在运行之前完成的,基本上我试图将运行时输入汇集到一些先前选择的编译时类型中。 Here's a high level example of what I'd like to be able to do. 这是我希望能够做的一个高级示例。

for (int i = 0; i < 2; ++i)
{
    std::vector<getRunTimeT(i)> v;

    // Do stuff with v
}

So, the vector should be initialized with a type selected by the runtime input. 因此,应使用运行时输入选择的类型初始化向量。

So far, I've come to the conclusion that specialized template meta-programming is needed, and I have functionality that does what I need with compile -time parameters: 到目前为止,我已经得出结论,需要专门的模板元编程,并且我有使用compile -time参数执行我需要的功能:

template<int i>
struct getCompileTimeT
{
    typedef int type; // Base case, not expected to occur.
};

template<>
struct getCompileTimeT<0>
{
    typedef int type;
};

template<>
struct getCompileTimeT<1>
{
    typedef float type;
};

So I can do this: 所以我可以这样做:

std::vector<getCompileTimeT(1)> type; // Creates a vector of floats

So, somehow I need to bridge the compile-time aspect with some function that selects the correct type from the predefined template types. 因此,我需要以某种方式将编译时方面与从预定义模板类型中选择正确类型的函数进行桥接。 Here is an ( illegal ) example of how I envision this bridging function to work: 这是一个( 非法 )示例,说明我如何设想这种桥接功能:

// Illegal demonstrative desired functionality
_type_ getRunTimeT(int select)
{
    if (select == 0) return getCompileTimeT<0>;
    else if (select == 1) return getCompileTimeT<1>;
}

So basically I am looking for something that 'bridges' between the compile-time functionality and the run-time functionality I've spoken of. 所以基本上我正在寻找在编译时功能和我所说的运行时功能之间“桥接”的东西。 Is this possible? 这可能吗?

Thanks for any help you can give! 谢谢你提供的所有帮助!

EDIT: Additional info: 编辑:附加信息:

Thank you for the solutions thus far; 感谢您迄今为止的解决方案; they have been very useful in helping me to understand this more thoroughly. 他们在帮助我更彻底地理解这一点方面非常有用。 I would like to provide an API-type function where a user can manipulate or use a type, which is 'dynamic' from their perspective. 我想提供一个API类型的函数,用户可以操作或使用一个类型,从他们的角度看是“动态的”。 For example: 例如:

UserClass::function()
{
    for (int i = 0; i < 2; ++i)
    {
        // getType() is something that may be a template parameter
        myFunc<getType(i)>();
    }
}

template<class T>
void UserClass::myFunc()
{
    std::vector<T> v;

    // Now the user has a vector to play with in their member function.
}

try this 试试这个

template <class T>
void do_stuff()
{
    std::vector<T> v;
    // Do stuff with v
}

void do_suff_dispatch(int i)
{
    switch (i)
    {
        case 0:
            do_stuff<int>();
            break;
        case 1:
            do_stuff<float>();
            break;
    }
}

for (int i = 0; i < 2; ++i)
{
    do_suff_dispatch(i);
}

You can consider using boost::variant<> as a type preserving generic value store: 您可以考虑使用boost :: variant <>作为保留通用值存储的类型:

http://www.boost.org/doc/libs/1_55_0/doc/html/variant/tutorial.html http://www.boost.org/doc/libs/1_55_0/doc/html/variant/tutorial.html

The idea goes as following: 这个想法如下:

typedef boost::variant<
    getCompileTimeT<0>,
    getCompileTimeT<1>
> variant_type;

variant_type getRunTimeT(int select)
{
    if (select == 0) return variant_type(getCompileTimeT<0>());
    else if (select == 1) return variant_type(getCompileTimeT<1>());
}

At this stage, you can rely on variant's visitor pattern (described in variant's tutorial) to do type safe value dispatch down the line. 在这个阶段,您可以依赖variant的访问者模式(在变体的教程中描述)来进行类型安全值调度。

Depending on your application and with a bit of template magic (especially on c++11) you can define the original variant type concisely over range of getCompileTimeT<> template parameters (so no need to enumerate all explicitly). 根据您的应用程序和一些模板魔法(特别是在c ++ 11上),您可以在getCompileTimeT<>模板参数范围内简明地定义原始变体类型(因此无需明确枚举所有模板参数)。

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

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