Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将元素数组转换为引用包装元素数组_C++_C++11_Templates_C++14 - Fatal编程技术网

C++ 将元素数组转换为引用包装元素数组

C++ 将元素数组转换为引用包装元素数组,c++,c++11,templates,c++14,C++,C++11,Templates,C++14,我有一些元素的数组。 我想创建一个函数,该函数将生成一个包含元素引用的引用包装器数组 具有如下界面的内容: template<typename T, size_t N> std::array<std::reference_wrapper<T>,N> wrap(std::array<T,N>& a); 模板 std::array wrap(std::array&a); 最好的方法是什么 注意:我不想使用vector。您可以这样做。这是一个

我有一些元素的数组。 我想创建一个函数,该函数将生成一个包含元素引用的引用包装器数组

具有如下界面的内容:

template<typename T, size_t N>
std::array<std::reference_wrapper<T>,N> wrap(std::array<T,N>& a);
模板
std::array wrap(std::array&a);
最好的方法是什么


注意:我不想使用vector。

您可以这样做。这是一个例子

名称空间详细信息{
样板
std::数组换行(std::数组和a,
std::索引(U序列){
返回{a[索引]…};
}
}
样板
std::array wrap(std::array&a){
返回详细信息::wrap(a,std::make_index_sequence());
}

你可以这样做。这是一个例子

名称空间详细信息{
样板
std::数组换行(std::数组和a,
std::索引(U序列){
返回{a[索引]…};
}
}
样板
std::array wrap(std::array&a){
返回详细信息::wrap(a,std::make_index_sequence());
}

我就知道,在编译时这样做应该是c++的一种魔力。谢谢我知道,在编译时这样做应该是c++的魔力。谢谢
namespace detail {
    template<typename T, std::size_t N, std::size_t... Index>
    std::array<std::reference_wrapper<T>, N> wrap(std::array<T, N>& a,
                                                  std::index_sequence<Index...>) {
        return {a[Index]...};
    }
}

template<typename T, size_t N>
std::array<std::reference_wrapper<T>,N> wrap(std::array<T,N>& a) {
    return detail::wrap(a, std::make_index_sequence<N>());
}