c++ - 演绎指南、模板和子对象 : which compiler is right?

标签 c++ templates language-lawyer c++17 template-argument-deduction

考虑以下片段:

struct S {
    S() {}

    template<typename B>
    struct T {
        T(B &&) {}
    };

    template<typename B>
    T(B &&) -> T<B>;
};

int main() {
    S::T t{0};
}

铿锵 accepts it而 GCC rejects the code出现以下错误:

prog.cc:10:5: error: deduction guide 'S::T(B&&) -> S::T' must be declared at namespace scope

这是有效的代码吗?哪个编译器是正确的,GCC 还是 Clang?

最佳答案

根据 http://en.cppreference.com/w/cpp/language/class_template_argument_deduction

User-defined deduction guides must name a class template and must be introduced within the same semantic scope of the class template (which could be namespace or enclosing class) and, for a member class template, must have the same access, but deduction guides do not become members of that scope.

所以 clang 似乎是正确的。

关于c++ - 演绎指南、模板和子对象 : which compiler is right?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600645/

相关文章:

c++ - 如何使用 libCurl 将访问 token 发送到服务器 API

c++ - 如何使用 Boost 实现测试套件和用例的组织?

c++ - 接受一组对象类型的所有组合可能性作为在 C++ 中运行的参数

c++ - 为什么引用类型的 sizeof 会给你类型的大小?

c++ - OpenMP性能

c++ - C++中如何从txt文件中获取数据

ruby-on-rails - 如何将 slim 用于邮件文本模板

c++ - 非成员模板友元函数默认参数值错误

c++ - 模板化友元声明在 g++ 5.4.0 下不起作用——编译器错误或错误代码?

c++ - C++ 中的条件表达式总是 bool 类型吗?