Ser*_*riy 2 c++ c++-concepts c++20
在下面的代码中,为什么第二个和第三个概念会产生编译错误?
#include <tuple>
template <class P>
concept IsPair1 = std::tuple_size<P>::value == 2;
template <class P>
concept IsPair2 = std::tuple_size_v<P> == 2;
template <class P>
concept IsPair3 = requires { typename std::tuple_size<P>; } && std::tuple_size_v<P> == 2;
constexpr bool intIsPair1 = IsPair1<int>; // OK, false
constexpr bool intIsPair2 = IsPair2<int>; // error: incomplete type 'std::tuple_size<int>' used in nested name specifier
constexpr bool intIsPair3 = IsPair3<int>; // error: incomplete type 'std::tuple_size<int>' used in nested name specifier
Run Code Online (Sandbox Code Playgroud)
/usr/local/Cellar/gcc/11.1.0_1/include/c++/11.1.0/tuple:1334:61: error: incomplete type 'std::tuple_size<int>' used in nested name specifier
1334 | inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
| ^~~~~
Run Code Online (Sandbox Code Playgroud)
我预计根据https://en.cppreference.com/w/cpp/language/constraints所有三个都会被评估为 false ,
通过将参数映射和模板实参替换到表达式 E 中来检查原子约束的满足情况。如果替换导致无效类型或表达式,则不满足约束。
| 归档时间: |
|
| 查看次数: |
360 次 |
| 最近记录: |