演绎指南,模板和子对象:哪个编译器正确? [英] Deduction guides, templates and subobjects: which compiler is right?

查看:50
本文介绍了演绎指南,模板和子对象:哪个编译器正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

struct S {
    S() {}

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

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

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

C语接受,而GCC

Clang accepts it while GCC rejects the code with the following error:

prog.cc:10:5:错误:推论指南'S :: T(B&&)-> S :: T'必须在名称空间范围内声明

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

这是有效的代码吗?GCC或Clang是哪种编译器是正确的?

Is this valid code? Which compiler is right, GCC or 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.

所以c似乎是正确的.

这篇关于演绎指南,模板和子对象:哪个编译器正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆