gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:09:55 25 4
gpt4 key购买 nike

考虑以下片段:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com