gpt4 book ai didi

c++ - 在运行时查找模板参数

转载 作者:太空狗 更新时间:2023-10-29 20:15:39 26 4
gpt4 key购买 nike

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

std::vector<int>::size_type

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

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

A<3> instance;
int number = instance::???? //<--- assigns 3 to number

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

谢谢

最佳答案

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

template<int i>
int get(const A<i> & instance)
{
return i;
}

关于c++ - 在运行时查找模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com