首页 > 解决方案 > 从基类重用模板变量

问题描述

让我们考虑这样的类层次结构

template<typename T>
struct Base
{
    template<typename...U>
    constexpr static bool SOME_TRAIT = std::is_constructible_v<T,U...>;
};

template<typename T>
struct Derived : Base<T>
{
    //some usage of SOME_TRAIT
};

要在其中使用它,Dervied我必须使用类似的东西:

template<typename...U>
constexpr static bool SOME_TRAIT = Base<T>::template SOME_TRAIT<U...>;

这可能很吵。是否有类似using成员函数的声明,但可用于SOME_TRAIT

标签: c++template-meta-programmingtypetraits

解决方案


没有。或者,更确切地说,您已经找到了最好的方法。

问题是,解析器需要对它进行SOME_TRAIT解释。using SOME_TRAIT不会向它提供更多关于<and >in是否SOME_TRAIT<foo>应该被解释为运算符或分隔符的信息。语言标准要求在定义的时候完成Derived,而不是等到Derived实际专门化和使用,所以它不能假设它SOME_TRAIT总是一个变量模板(因为Base稍后可能会被赋予专门化)。bool除了和命名之外,您在那里提供U的是解析器完成其工作所需的最少信息量。


推荐阅读