我正在学习 C++ 概念中的嵌套需求。考虑以下代码片段:
#include <concepts>
#include <iostream>
// Nested requirement
template <typename... Ts>
concept VariadicAddable = requires(Ts... vs)
{
(... + vs); // + operator is provided
requires sizeof...(Ts) > 1; // Atleast two arguments are provided
};
template <VariadicAddable... Ts>
auto add_variadic(Ts... vs)
{
return (... + vs);
}
int main()
{
std::cout << add_variadic(1, 2) << std::endl; // Expecting no failure here
//std::cout << add_variadic(1) << std::endl; // Expecting failure here
}
编译这个时我遇到错误...(Godbolt,msvc vs17.10 带有 c++20 选项)
<source>(20): error C2672: 'add_variadic': no matching overloaded function found
<source>(13): note: could be 'auto add_variadic(Ts...)'
<source>(20): note: the associated constraints are not satisfied
<source>(12): note: the constraint was not satisfied
我还尝试了指定相同约束的变体,如下所示:
template <typename... Ts>
concept VariadicAddable = (sizeof...(Ts) > 1) && requires(Ts... vs)
{
(... + vs); // + operator is provided
};
在这种情况下也会出现类似的错误。知道我遗漏了什么吗?