c++ - 带有尾随 requires 子句的类外模板成员函数定义的允许形式

标签 c++ c++20 c++-concepts

考虑以下代码,其中类 A有一个嵌套类型 B使用包含尾随 requires 子句的模板成员函数命名嵌套类型 B , 随后定义了类外:

template <typename X, typename Y>
concept C = true;

struct A
{
    struct B {};

    template <typename T>
    void f1()
    requires C<T, A::B>;

    template <typename T>
    void f2()
    requires C<T, A::B>;

    template <typename T>
    void f3()
    requires C<T, B>;

    template <typename T>
    void f4()
    requires C<T, B>;
};

template <typename T>
inline void A::f1()
requires C<T, A::B> {}

template <typename T>
inline void A::f2()
requires C<T, B> {}

template <typename T>
inline void A::f3()
requires C<T, A::B> {}

template <typename T>
inline void A::f4()
requires C<T, B> {}

int main()
{
    A{}.f1<A::B>();
    A{}.f2<A::B>(); 
    A{}.f3<A::B>();
    A{}.f4<A::B>();
}

我一直无法找到/理解关于是否:

  • 尾随 requires 子句可以命名嵌套类型,而无需以类似于尾随返回类型的方式进行显式限定
  • f2 中的哪一个, f3 , 和 f4 ,如果有的话,应该被一个符合标准的实现所接受

我能在标准草案中找到的最接近的是 [temp.mem],

A member template of a class template that is defined outside of its class template definition shall be specified with a template-head equivalent to that of the class template followed by a template-head equivalent to that of the member template (13.7.6.1).

13.7.6.1 在第 7 段中引用 [temp.over.link],

Two function templates are equivalent if they are declared in the same scope, have the same name, have equivalent template-heads, and have return types, parameter lists, and trailing requires-clauses (if any) that are equivalent using the rules described above to compare expressions involving template parameters.

就 requires 子句本身而言,等效性似乎由

they both have requires-clauses and the corresponding constraint-expressions are equivalent.

在任何其他情况下,我希望 f1 中的所有形式的约束通过f4是(正式地)等效,但我对标准还不够熟悉,无法为自己下结论。

在实现方面,clang 和 gcc 似乎始终接受所有定义,而 MSVC 不同,并且最近在行为上发生了变化:

<表类="s-表"> <头> 函数 gcc 12.2 clang 15.0.0 MSVC 19.33 MSVC Latest (19.34?) <正文> f1 已接受 已接受 已接受 已接受 f2 已接受 已接受 error C2244: 'A::f2': 无法将函数定义与现有声明相匹配 error C2244: 'A::f2': 无法将函数定义与现有声明相匹配 f3 已接受 已接受 error C2244: 'A::f3': 无法将函数定义与现有声明相匹配 error C2244: 'A::f3': 无法将函数定义与现有声明相匹配 f4 已接受 已接受 已接受 error C2065: 'B': 未声明的标识符

最佳答案

我引用的是当前草案而不是 C++20 草案,因为对相关部分进行了重大修订,使它们更加清晰。

根据 [basic.scope.class]/1成员函数的(非 friend)重新声明的 declarator-id 之后的所有内容都在类的范围内,因此应该能够查找 B具有不合格的名称。尾随 requires-clause 出现在 declarator-id A::fX 之后.

不过我认为是因为模板参数T用于约束表达式 sentence 1 of [temp.over.link]/2 apply 表示表达式 C<T, A::B>C<T, B>仅当在两个函数定义中使用它们满足 ODR 时它们才等价,但模板参数可以重命名。但是 ODR 不允许使用不同的 token 序列来命名 A::B .

但由于这两个表达式在功能上仍然是等价的,这将使它们不匹配的情况不合式;不需要诊断 (IFNDR),这意味着 f2f3将是无效的,尽管不需要诊断。这是因为必须检查等价性以确定类外定义是否是已在类中声明的成员的重新声明。如果两个结构在功能上是等价的,但当等价会影响程序的语义时不等价,那么该程序就是 IFNDR。

我认为等价规则有时很难遵循,所以我的看法可能不正确。但似乎 MSVC 遵循类似的解释,我怀疑 'B': undeclared identifier最新版本中的错误只是回归。其他编译器的行为也与此解释兼容。

关于c++ - 带有尾随 requires 子句的类外模板成员函数定义的允许形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74565443/

相关文章:

c++ - 使用预处理器生成具有多个参数的显式实例化

c++ - 如何简洁地表达包含 T 类型值的范围的 C++20 概念?

c++ - 在 requires 参数列表中,您可以引入导致替换失败的类型吗?

c++ - 为哈希函数定义 c++20 概念

android inotify_add_watch 失败 : permission denied?

时间:2019-03-09

c++ - 移除对 boost::filesystem::current_path() 的依赖

c++ - 在 C++20 之前将 malloc 用于 int 未定义行为

c++ - 要求约束必须评估为 bool。所以没有 SFINAE

c++ - 使用概念禁用非模板化方法