使用语法在派生类中公开基类别名模板和变量模板?

在繁重的模板元编程上下文中考虑基础模板类和派生模板类(此处为便于阅读并着重于此问题而对其进行了简化)。

template <class T>
struct base {
    using type = T;
    static constexpr int value = 0;
    template <class... Args>
    constexpr void function(Args&&...) {}
    template <class U>
    using alias_template = base<U>;
    template <class U>
    static constexpr int variable_template = 0; 
};

template <class T>
struct derived: base<T> {
    using typename base<T>::type;           // Using base typedef
    using base<T>::value;                   // Using base static data member
    using base<T>::function;                // Using base function members (regardless of template or not)
    //using typename base<T>::alias_template; // DOES NOT SEEM TO WORK
    //using base<T>::variable_template;       // DOES NOT SEEM TO WORK

    using typedef_test = type;                                          // Working
    static constexpr int value_test = value;                            // Working
    using alias_template_test = alias_template<T>;                      // ERROR
    static constexpr int variable_template_test = variable_template<T>; // ERROR
};

QUESTION: Is there a using syntax to expose alias templates and variables templates inherited from a base class in order to make the currently erroneous lines compile? Is there any workaround to avoid to specify base<T>:: every single time in the derived class (here it remains simple, but in my actual code, specifying everytime rapidly becomes annoying)?

评论
吐泡泡oo
吐泡泡oo

using doesn't work this way for dependent member templates:

using声明也不能用于将从属成员模板的名称作为模板名称引入(不允许从属名称的模板消歧器)

我不确定这是否满足您的要求,但是为了使别名像模板一样工作,您可以在根据基本成员模板定义的派生类中声明新的成员模板:

template<typename U>
using alias_template = typename base<T>::template alias_template<U>;

template<typename U>
static constexpr auto variable_template = base<T>::template variable_template<U>;
点赞
评论