[英]Find template parameters at runtime
可以访问模板“type”,例如在std中
std::vector<int>::size_type
是否可以为作为模板参数传递的对象具有相同的功能? 例如:
template<int i>
class A {
//?
};
A<3> instance;
int number = instance::???? //<--- assigns 3 to number
是否有可能在运行时再次传入对象类型? 不在A类中创建特定成员(这会增加对象的大小)
谢谢
编译器在编译时知道变量的类型,只需要让它放弃即可。
template<int i>
int get(const A<i> & instance)
{
return i;
}
template<int i>
class A {
public:
enum { number = i };
};
int main() {
A<3> instance;
std::cout << instance.number;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.