考虑以下片段:
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/