在具有临时基的派生类中对类型别名模板使用声明

Evg*_*Evg 8 c++ templates using c++11

如果基类依赖于模板参数,则不会在非限定名称查找中检查其范围。我可以使用该using声明从基类中引入名称。假设现在我在基类中有一个类型别名模板。可以使用该using声明将其引入派生类吗?

template<class T>
struct Base
{
    using Type1 = int;

    template<typename S>
    using Type2 = S;
};

template<class T>
struct Derived : Base<T>
{
    using typename Base<T>::Type1;           // Fine
    //using Type1 = typename Base<T>::Type1; // Also fine

    template<typename S>
    using Type2 = typename Base<T>::template Type2<S>;
};
Run Code Online (Sandbox Code Playgroud)

可以Type2用类似于(未注释)的行替换行Type1吗?

Jan*_*tke 0

template<class T>
struct Derived : Base<T>
{
    using Base<T>::Type2;

    void foo() {
        // Using the Type2 alias template
        // inside our class to declare a local variable.
        typename Derived::template Type2<int> i;
    }
};

// Using the Type2 alias template
// outside the class to declare another type.
using X = Derived<int>::Type2<int>;
Run Code Online (Sandbox Code Playgroud)

这是正确的语法,所有支持 C++11 的编译器都应该允许这样做。请参阅此处的现场演示

我的意思是这样简单的:using Base<T>::Type2;,可能带有typenames 和templates。

我们在声明中不需要这些消歧符using,但是当我们想要使用Type2in时Derived,我们仍然需要:

  • template歧器因为Type2是一个模板并且Derived依赖于T
  • typename指定它Type2<...>是一种类型的消歧符

另请参阅:有关从属名称的 cppreference 文章