类模板的require子句是否必须在成员定义之外重复? [英] Does a class template's requires clause have to be repeated outside member definitions?

查看:65
本文介绍了类模板的require子句是否必须在成员定义之外重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在类外定义了使用 requires 子句的类模板的成员时,如果 requires 不是,则 gcc 不会抱怨指定,而 clang 则指定.

When the member of a class template that uses the requires clause is defined outside the class, gcc does not complain if requires is not specified, whereas clang does.

请考虑以下代码段:

#include <concepts>

template<typename Container>
    requires std::integral<typename Container::value_type>
class Foo {
public:
    void func();
};

template<typename Container>
void Foo<Container>::func()
{}

使用 gcc 进行编译不会产生错误.

The compilation using gcc does not complain.

clang 报告以下错误:

❯ clang++ -std=c++2a test.cpp
test.cpp:10:1: error: requires clause differs in template redeclaration
template<typename Container>
^
test.cpp:4:19: note: previous template declaration is here
    requires std::integral<typename Container::value_type>
                  ^
1 error generated.

如果我按如下所示更改定义:

If I change the definition as below:

template<typename Container>
    requires std::integral<typename Container::value_type>
void Foo<Container>::func()
{}

现在 clang 不抱怨.

gcc --version 的输出:

gcc (GCC) 10.2.0

clang --version 的输出:

Output from clang --version:

clang version 10.0.1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

这是一个要报告的错误吗?

Is this a bug to be reported?

推荐答案

即使类外部成员的声明没有等效的模板头,也应为GCC提交错误,因为它会接受代码.

A bug should be filed for GCC, because it accepts the code, even though the declaration of the member outside the class does not have an equivalent template-head.

[临时班级]

3 当一个成员函数,一个成员类,成员枚举静态数据成员或类模板的成员模板是在类模板定义之外定义,成员定义被定义为模板定义,其中 template-head 等同于类模板的([temp.over.link]).

3 When a member function, a member class, a member enumeration, a static data member or a member template of a class template is defined outside of the class template definition, the member definition is defined as a template definition in which the template-head is equivalent to that of the class template ([temp.over.link]).

[临时链接]

6 如果两个模板头相等他们的template-parameter-list具有相同的长度,对应template-parameters是等效的,并且都用类型约束,如果其中一个模板参数为用类型约束声明,如果任意一个模板头都具有一个要求条款,他们都有要求条款,并且相应的约束表达式是等效的.

6 Two template-heads are equivalent if their template-parameter-lists have the same length, corresponding template-parameters are equivalent and are both declared with type-constraints that are equivalent if either template-parameter is declared with a type-constraint, and if either template-head has a requires-clause, they both have requires-clauses and the corresponding constraint-expressions are equivalent.

等价的模板头要求它们都具有一个等效的require子句.完全省略它会等效.

The equivalence of templates-heads requires that both have an equivalent requires clause. Omitting it entirely breaks the equivalence.

这篇关于类模板的require子句是否必须在成员定义之外重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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