ngm*_*r80 6 c++ c++-concepts c++20 non-type-template-parameter
在 C++20 中,我定义了一个AllIntegral带有非类型模板参数包的概念auto... T_values。虽然 GCC 10.1.0 在某些上下文中接受它的用法,但它拒绝编译它在其他上下文中的用法,尤其是在
if语句中。相关的错误消息显示“AllIntegral”不限制类型。
我的代码如下所示:
#include <concepts>
#include <ios>
#include <iostream>
template<auto... T_values>
concept AllIntegral = (std::integral<decltype(T_values)> && ...);
int main()
{
std::cout << std::boolalpha << AllIntegral<1, 2> << '\n'; // compiles and prints "true"
if (AllIntegral<1, 2>) std::cout << "true" << '\n'; // does not compile
std::cout.flush();
}
Run Code Online (Sandbox Code Playgroud)
这是编译器输出:
main.cpp: In function ‘int main()’:
main.cpp:11:9: error: ‘AllIntegral’ does not constrain a type
11 | if (AllIntegral<1, 2>) std::cout << "true" << '\n';
| ^~~~~~~~~~~~~~~~~
main.cpp:6:9: note: concept defined here
6 | concept AllIntegral = (std::integral<decltype(T_values)> && ...);
Run Code Online (Sandbox Code Playgroud)
这个错误的原因是什么?为什么我的概念在布尔上下文中不可用?
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |