首页 文章

使类模板强制在继承构造函数中重复基类模板参数

提问于
浏览
3

我最近将一个类从模板改为不,并发现在编写using声明从模板化基类继承构造函数时,我不能再省略模板参数 . 只要我的 class 不是't templated I can omit the arguments, as soon as it is I can' t . 在下面的可编辑片段中, bar 表示之前的类,而 buzz 表示之后的类 . 我测试了GCC 5.2和Clang 3.7,它们具有相同的行为 . 这是编译器错误还是标准?

#include <iostream>

template<class A, class B>
struct foo {
    foo(int x) {
        std::cout << x << std::endl;
    }
};

struct bar : foo<bar, short> {
    using foo::foo; // this appears legal
    // using foo<bar, short>::foo; // this works too, thought I would need it
};

template<class X>
struct buzz : foo<buzz<X>, short> {
     //using foo::foo; // no longer legal for some reason
    using foo<buzz<X>, short>::foo; // now do actually need this
};

int main() {
    bar x(3);
    buzz<float> y(5);
    return 0;
}

1 回答

  • 5

    这是标准的 .

    N4140 [temp.dep] / 3:在类或类模板的定义中,如果基类依赖于模板参数,则在非限定名称查找期间,不会在定义时检查基类作用域 . 类模板或成员或在类模板或成员的实例化期间 .

    对于 buzz ,基类依赖于模板参数,因此 foo 非限定查找不会检查其范围 . 这就是您需要合格查找的原因 .

相关问题