如何实现我想要的下面?我要解包的参数包不在函数参数列表中,而是在模板参数列表中。
#include <iostream>
#include <array>
const std::size_t SIZE = 10;
template <int...ARGS>
std::array<bool, SIZE> func() {
std::array<bool, SIZE> b;
// I want to set b[n] = true, where n takes on all values from ARGS...
// what to put in here???
return b;
}
// Example of what I want to achieve:
int main() {
const std::array<bool, SIZE> b = func<1,3,7>();
// I want b[1]==true, b[3]==true, b[7]==true, all others false
for (int x: b) std::cout << x << std::endl;
}
我必须为 func 使用这种形式(而不是 func(1,3,7))才能让我的更大程序工作(我正在处理多重继承问题)。
原文由 prestokeys 发布,翻译遵循 CC BY-SA 4.0 许可协议
递归模板解决方案:
编辑: 受iavr解决方案启发的超级简化版本: