C++ 初始化多个C++-具有公共数据的数组

C++ 初始化多个C++-具有公共数据的数组,c++,arrays,c++11,initialization,C++,Arrays,C++11,Initialization,我有几个常量C++数组,我想用不同的数据初始化它们,但是前缀总是一样的。此示例编译: const int array_1[] = { 1, 2, 3, 5, 5 }; const int array_2[] = { 1, 2, 3, 8, 7, 6 }; // ... 是否可以不每次指定前缀(1、2、3)?这将编译并演示它,但使用宏的缺点是: #define prefix 1, 2, 3 const int array_1[] = { prefix, 5, 5 }; const int arr

我有几个常量C++数组,我想用不同的数据初始化它们,但是前缀总是一样的。此示例编译:

const int array_1[] = { 1, 2, 3, 5, 5 };
const int array_2[] = { 1, 2, 3, 8, 7, 6 };
// ...
是否可以不每次指定前缀(
1、2、3
)?这将编译并演示它,但使用宏的缺点是:

#define prefix 1, 2, 3
const int array_1[] = { prefix, 5, 5 };
const int array_2[] = { prefix, 8, 7, 6 };
要求:

  • 没有宏
  • 您可以使用C++11,如果需要,也可以使用C++14。请指定所需的版本
  • std::array
    而不是C样式的数组是允许的
C++11:

#include <array>
#include <utility>

template<int... Is> struct seq {};
template<int N, int... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template<int... Is> struct gen_seq<0, Is...> : seq<Is...> {};

template<class T, int N, class... Rs, int... Is>
constexpr auto append(seq<Is...>, T (&lhs)[N], Rs&&... rhs)
-> std::array<T, N+sizeof...(Rs)>
{
    return {{lhs[Is]..., std::forward<Rs>(rhs)...}};
}

template<class T, int N, class... Rs>
constexpr auto append(T (&lhs)[N], Rs&&... rhs)
-> decltype( append(gen_seq<N>{}, lhs, std::forward<Rs>(rhs)...) )
{
    return append(gen_seq<N>{}, lhs, std::forward<Rs>(rhs)...);
}

constexpr int prefix[] = {1,2,3};
constexpr auto array_1 = append(prefix, 5, 5);
constexpr auto array_2 = append(prefix, 8, 7, 6);

#include <iostream>
int main()
{
    std::cout << "array_1: ";
    for(auto const& e : array_1) std::cout << e << ", ";
    std::cout << "\n";

    std::cout << "array_2: ";
    for(auto const& e : array_2) std::cout << e << ", ";
}
#包括
#包括
模板结构seq{};
模板结构gen_seq:gen_seq{};
模板结构gen_seq:seq{};
模板
constexpr自动附加(序列、T和lhs)[N],序列和…rhs)
->std::数组
{
返回{lhs[Is]…,std::forward(rhs)…};
}
模板
constexpr自动附加(T(&lhs)[N],Rs&…rhs)
->decltype(追加(gen_seq{},lhs,std::forward(rhs)…)
{
返回附加(gen_seq{},lhs,std::forward(rhs)…);
}
constexpr int前缀[]={1,2,3};
constexpr auto array_1=append(前缀5,5);
constexpr auto array_2=append(前缀8、7、6);
#包括
int main()
{
std::coutC++11:

#包括
#包括
模板结构seq{};
模板结构gen_seq:gen_seq{};
模板结构gen_seq:seq{};
模板
constexpr自动附加(序列、T和lhs)[N],序列和…rhs)
->std::数组
{
返回{lhs[Is]…,std::forward(rhs)…};
}
模板
constexpr自动附加(T(&lhs)[N],Rs&…rhs)
->decltype(追加(gen_seq{},lhs,std::forward(rhs)…)
{
返回附加(gen_seq{},lhs,std::forward(rhs)…);
}
constexpr int前缀[]={1,2,3};
constexpr auto array_1=append(前缀5,5);
constexpr auto array_2=append(前缀8、7、6);
#包括
int main()
{

对我来说,除了使用宏,我没有其他的想法

    #define COMMON_PART 1, 2, 3
    const int array_1[] = { COMMON_PART, 5, 5 };
    const int array_2[] = { COMMON_PART, 8, 7, 6 }; 

至于我,除了使用宏,我没有其他的想法

    #define COMMON_PART 1, 2, 3
    const int array_1[] = { COMMON_PART, 5, 5 };
    const int array_2[] = { COMMON_PART, 8, 7, 6 }; 

它们必须是原始的(C样式)数组吗?还是<代码> STD::数组< /代码> s也是可能的?@ DYPSTD::数组将是好的。如果使用它,请只给出需要C++版本的要求。它们必须是原始的(C样式)吗?数组?还是<代码> STD::数组也可以吗?@ DYPSTD::数组将很好。如果使用它,请只给出需要C++版本的要求。OP@VladfromMoscow这是在原始帖子中提到的;)可能吧。这意味着我没有专心。不符合OP@VladfromMoscow这是在原始帖子中提到的;)可能吧。这意味着我没有注意到。谢谢,这很好!我希望std库中已经有这样的东西。如果没有更少代码的解决方案,我会在接下来的几天内接受。@Johannes在C++1y中,我们将获得
seq
和序列生成(
index_sequence
make_index_sequence
)以及返回类型推断(对于普通函数)。这将代码从11行减少到6行。谢谢,这非常有效!我希望std库中已经有这样的东西。如果没有代码更少的解决方案,我将在接下来的几天内接受。)@Johannes在C++1y中,我们将获得
seq
和序列生成(
index\u sequence
make\u index\u sequence
)以及返回类型推断(对于普通函数)。这将代码从11行减少到6行。