是类模板中所需的模板预选赛非依赖性成员变量



我得到了编译错误"错误"错误:当我在注册行上编译以下代码时,请使用'模板'关键字将'foo'视为依赖模板名称。(test4(

代码的所有其他部分都已成功编译。

#include <tuple>
struct my {
    template <typename T>
    void foo() {}
};
void test1() {
    my m;
    auto tr = std::forward_as_tuple(m);
    auto& t0 = std::get<0>(tr);
    t0.foo<int>();
}
template <typename T>
struct test2 {
    void method() {
        my m;
        auto tr = std::forward_as_tuple(m);
        auto& t0 = std::get<0>(tr);
        t0.foo<int>();
    }
};
template <typename T>
struct test3 {
    void method() {
        m.foo<int>();
    }
    my m;
};
template <typename T>
struct test4 {
    void method() {
        auto tr = std::forward_as_tuple(m);
        auto& t0 = std::get<0>(tr);
        t0.foo<int>();          // error: use 'template' keyword to treat 'foo' as a dependent template name
        t0.template foo<int>(); // OK
    }
    my m;
};
template <typename T>
struct test5 {
    void method() {
        std::tuple<my> tr = std::forward_as_tuple(m);
        auto& t0 = std::get<0>(tr);
        t0.foo<int>();
    }
    my m;
};
template <typename T>
struct test6 {
    void method() {
        auto tr = std::forward_as_tuple(m);
        my& t0 = std::get<0>(tr);
        t0.foo<int>();
    }
    my m;
};

int main() {
    test1();
    test2<int>().method();
    test3<int>().method();
    test4<int>().method();
    test5<int>().method();
    test6<int>().method();
}

test4是类模板,但m是非依赖类型。

我试图编译GCC和Clang。GCC 7.1.0没有报告错误,但clang 4.0及以后报告编译错误。

错误

https://wandbox.org/permlink/htsbjmd2kxwfwobl(clang 4.0(https://wandbox.org/permlink/bcut8gtafxwc41c5(clang head(

无错误

https://wandbox.org/permlink/gjivza3i5hb8uh6w(GCC 7.1.0(

哪个是正确的行为?

我将同意您的怀疑。这确实是一个叮当的错误。

仅当t0是因名称时,才需要template。特别是,如果t0取决于T,则需要。这就是test4<T>中的T

现在,t0取决于my m,并且有一个my::foo<T>,但这是不同范围中无关的T。此外,t0不依赖my::foo<T>

最新更新