8

Consider this code:

#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 compiles it fine and prints 10.

But §14.10.1.2 Predicate constraints [temp.constr.pred] of N4553 says

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

and then

After substitution, E shall have type bool.

Since C1<decltype(std::declval<A>() + std::declval<A>())> is a substitution failure, rather than having type bool, does that mean the program should be ill-formed?

4

1 回答 1

4

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

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

民意调查:SF | F | N | 一个 | 南非

  • 我们应该允许在任何地方评估概念吗?8 | 6 | 2 | 0 | 0
  • 我们是否应该允许在任何表达式中出现和求值 requires 表达式?1 | 2 | 10 | 3 | 1
    • 请注意,如果没有第一次投票,第二次投票将会改变。

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

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

于 2015-12-06T00:19:15.533 回答