c++ - 在概念定义中,是否允许在 requires 表达式之外出现替换失败?

标签 c++ c++17 c++-concepts

考虑这段代码:

#include <type_traits>                                           
#include <iostream>

template <class T> concept bool C1 = std::is_same<T, int>::value; 

template <class T> concept bool C2 =  
    C1<decltype(std::declval<T>() + std::declval<T>())>; 

struct A {};                   

int main() { 
  std::cout << C2<int>; 
  std::cout << C2<A>;                                                 
  return 0;                                                           
}

GCC 编译它 fine并打印 10。

但是 §14.10.1.2 N4553 的谓词约束 [temp.constr.pred]

A predicate constraint is a constraint that evaluates a constant expression E (5.19).

然后

After substitution, E shall have type bool.

C1<decltype(std::declval<A>() + std::declval<A>())>是替换失败,而不是 bool 类型,这是否意味着程序应该是病式的?

最佳答案

Concepts TS 仅定义用于确定声明的相关约束是否满足的行为;没有规定在关联约束之外引用概念名称。所以严格来说,std::cout << C<int>std::cout << C<A>都是病式的。

EWG 决定在 Kona 中将此作为一项新功能:

Straw poll: SF | F | N | A | SA

  • Should we allow evaluation of concepts anywhere? 8 | 6 | 2 | 0 | 0
  • Should we allow the presence and evaluation of a requires-expression in any expression? 1 | 2 | 10 | 3 | 1
    • Note that without the first poll, the second poll would change.

但目前还没有具体说明其行为的措辞。

GCC 目前允许将概念作为表达式作为(我相信未记录的)扩展。我发现很可能会指定此功能,以便 C<X...>评估为 false替换 X... 时进入 C 的初始化程序未能产生有效的表达式,否则具有如此获得的表达式的值。这似乎是执行此操作的明智方法,并且与 GCC 中的实现一致。

关于c++ - 在概念定义中,是否允许在 requires 表达式之外出现替换失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34108878/

相关文章:

c++ - C++ n2660中的单调性是什么意思?

c++ - x86_32/64 中指针和整数之间的转换

c++ - 右值限定方法和 const 表达式

c++ - Concepts-Lite iterator comparison 比较值

c++ - C++ 概念是否允许我的类在声明/定义时指定它满足特定概念?

C++ 概念 : Some signatures function conversion

c++ - 使用 C++ 的 STL 列表 ....传递列表指针数组

c++ - 友元函数 = 两个不同类的运算符重载

c++ - 如何让非成员 get<N> 为命名空间中的自定义类工作 [C++17]

c++ - C++ 计时的逻辑错误(使用 std::chrono)