template <typename>
struct B
{
constexpr static int T = 5;
};
template <int T>
struct D : B<int>
{
constexpr static int value = T;
};
int main()
{
std::cout << D<7>::value << std::endl; // 5, how to get 7 ?
}
正如我最近了解到的,模板派生类的模板参数在查找期间会在基类中的名称之后进行检查。话虽如此,是否有qualify名称T
初始化value
来引用模板参数T
派生类?
编辑:
到目前为止,从评论中的讨论来看,似乎实现这一点的唯一方法是使基类类型/值依赖,这将延迟对基类名称的查找(到实例化阶段),从而使T 唯一可用的值是模板参数。
最佳答案
我不完全确定我理解了这个问题,但我认为 decltype
可以满足您的要求:
template <int T>
struct D : B<decltype(T)>
{
constexpr static decltype(T) value = T;
};
关于C++模板继承隐藏模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50639070/