在运行时查找模板参数

lez*_*lon 3 c++ templates

可以访问模板"type",例如在std中

std::vector<int>::size_type
Run Code Online (Sandbox Code Playgroud)

是否可以为作为模板参数传递的对象具有相同的功能?例如:

template<int i>
class A {
//?
};

A<3> instance;
int number = instance::???? //<--- assigns 3 to number
Run Code Online (Sandbox Code Playgroud)

是否有可能在运行时再次传入对象类型?不在A类中创建特定成员(这会增加对象的大小)

谢谢

Mar*_*som 5

编译器在编译时知道变量的类型,只需要让它放弃即可.

template<int i>
int get(const A<i> & instance)
{
    return i;
}
Run Code Online (Sandbox Code Playgroud)