C++ c++;从模板参数解包参数包

C++ c++;从模板参数解包参数包,c++,parameters,pack,C++,Parameters,Pack,如何达到我下面想要的?我要解包的paramater包不在函数参数列表中,而是在模板参数列表中 #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

如何达到我下面想要的?我要解包的paramater包不在函数参数列表中,而是在模板参数列表中

#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;
}
#包括
#包括
常数std::size\u t size=10;
模板
std::array func(){
std::数组b;
//我想设置b[n]=true,其中n接受ARGS中的所有值。。。
//在这里放什么???
返回b;
}
//我想要实现的示例:
int main(){
常量std::数组b=func();
//我想要b[1]==真,b[3]==真,b[7]==真,所有其他的都是假的
有关(intx:b)std::cout的信息,请参见

下面是
func
的实现:

template <int... A, int... N>
std::array<bool, sizeof...(N)>
func(sizes <N...>)
{
    return std::array<bool, sizeof...(N)>{{in <N, A...>()...}};
}

template <int... A>
std::array<bool, SIZE>
func() { return func <A...>(range <SIZE>()); }
模板
std::数组
func(尺寸)
{
返回std::数组{{in()…};
}
模板
std::数组
func(){return func(range());}
其中,
size
表示一个
int
序列,
range
构造序列
0,…,S-1
in()
检查数字
N
是否在序列
A…
(实例中的定义)


这不是最有效的(编译方式)实现方法,因为对于
N..
的每个元素,我们都需要扫描pack
A..
。最好是并行扫描pack
A..
L..
,并修改()
。但无论如何,这更容易思考和写下来。

递归模板解决方案:

// recursive helper struct
template <int n, int First, int ...Rest>
struct helper {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
    helper<n - 1, Rest...>::funcImpl(temp);
  }
};

// partial specialization to catch base case
template <int First>
struct helper<0, First> {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
  }
};

template <int ...Args>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {}; // 0 inititalize array
    helper<sizeof...(Args) - 1, Args...>::funcImpl(b);
    return b;
}
template <int... A>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {};
    auto values = {A...};
    std::for_each(values.begin(), values.end(), [&](int n){b[n] = true;});
    return b;
}
//递归帮助器结构
模板
结构辅助程序{
静态void funcImpl(标准::数组和临时){
温度[第一]=真;
助手::funcImpl(临时);
}
};
//部分专门化以捕获基本情况
模板
结构辅助程序{
静态void funcImpl(标准::数组和临时){
温度[第一]=真;
}
};
模板
std::array func(){
std::数组b={};//0初始化数组
helper::funcImpl(b);
返回b;
}
编辑:受iavr解决方案启发的超简化版本:

// recursive helper struct
template <int n, int First, int ...Rest>
struct helper {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
    helper<n - 1, Rest...>::funcImpl(temp);
  }
};

// partial specialization to catch base case
template <int First>
struct helper<0, First> {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
  }
};

template <int ...Args>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {}; // 0 inititalize array
    helper<sizeof...(Args) - 1, Args...>::funcImpl(b);
    return b;
}
template <int... A>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {};
    auto values = {A...};
    std::for_each(values.begin(), values.end(), [&](int n){b[n] = true;});
    return b;
}
模板
std::array func(){
std::数组b={};
自动值={A..};
std::for_each(values.begin()、values.end()、[&](int n){b[n]=true;});
返回b;
}

这里有一个简单得多的解决方案,它不需要额外的东西,只需要:

struct _do { template <typename... T> _do(T&&...) { } };

template <int... A>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {};
    _do{b[A] = true...};
    return b;
}
struct_do{template_do(T&&…{});
模板
std::array func(){
std::数组b={};
_do{b[A]=true…};
返回b;
}

这假设数组是先初始化然后填充的。我以前的解决方案在编译时计算所有值,并直接用它们初始化数组。因此,这可能会加快编译速度,但运行速度较慢。

这个可能最优雅的解决方案将真值设置为正确值,但剩余值不会设置为false。我初始化正如您所建议的:std::array b={false};b=func();如果在func中更改为std::array b={};它现在可以工作definition@prestokeys我真的认为因为
std::array
是一种聚合类型,所以它的元素是默认初始化的;,谢谢。也许在编译时不是最快的,但以
constexpr
方式初始化数组的能力(无论是这种模式还是非常类似的模式)都是非常宝贵的。+1最后一个解决方案非常漂亮。