小编yua*_*liu的帖子

如何使用模板和概念使函数接受某些参数

我想编写一个使用常量模板接受一定数量参数的函数,例如:

template<int count>
void foo(int... args) { ... }

foo<1>(0); // success
foo<2>(0); // error
foo<3>(0, 0, 0); // success
Run Code Online (Sandbox Code Playgroud)

count在模板中决定它可以接受多少个参数。

我尝试写一个这样的原型

template <int a, int b>
concept equal = (a == b);

template <int size>
void foo(int... args) requires equal<sizeof...(args), size>
{ ... }
Run Code Online (Sandbox Code Playgroud)

它没有遵守。正确的方法是什么,或者有更简单的方法。

非常感谢

c++ templates c++-concepts c++20

5
推荐指数
1
解决办法
129
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

templates ×1