如何在初始化时将不同数量的对象包括在初始化列表中?

问题描述

我需要根据提供的“定义”添加不同数量的对象 并具有不同的ctor参数

inline static std::array<A::Output,NUM_OUTPUTS> s_Outputs =
{
#if NUM_OUTPUTS > 0
    A::Output{0}
#endif
#if NUM_OUTPUTS > 1,A::Output{1}
#endif
#if NUM_OUTPUTS > 2,A::Output{2}
#endif
};

应该根据NUM_OUTPUTS创建对象的相应编号。每个对象都有一个索引,第一个是'0',第二个是'+1'。

是否有一种更好的方法可能是宏会在此类声明或其他内容中推出。

解决方法

使用std::make_index_sequencevariable template就可以了。

#include <utility>  // std::integer_sequence

template <std::size_t... I>
constexpr auto makeArray(std::index_sequence<I...>)
{
   return std::array<A::Output,sizeof...(I)>{I...};
}
template<std::size_t NUM_OUTPUTS>
constexpr static std::array<A::Output,NUM_OUTPUTS> s_Outputs = makeArray(std::make_index_sequence<NUM_OUTPUTS>{});

现在您可以

constexpr auto arr1 = s_Outputs<1>;  // 0
constexpr auto arr2 = s_Outputs<2>;  // 0 1
constexpr auto arr3 = s_Outputs<3>;  // 0 1 2

See a Demo Online

请注意,以上解决方案需要编译器支持。

,

您可以定义各种模板,并根据预处理器变量选择适当的专业化:

template<std::size_t I>
class outputs;

template<>
class outputs<1> {
    static std::array<A::Output,1> value = {{0}};
};

template<>
class outputs<2> {
    static std::array<A::Output,2> value = {{0},{1}};
};

template<>
class outputs<3> {
    static std::array<A::Output,3> value = {{0},{1},{2}};
};

inline static auto s_Outputs = outputs<NUM_OUTPUTS>::value;

对于更通用的变体,您可以结合使用std::index_sequence和一些辅助功能(C ++ 17代码):

template<std::size_t... Ints>
decltype(auto) make_integer_array_helper(std::index_sequence<Ints...>) {
   return std::array { A::Output{Ints...} };
}

template<std::size_t N>
decltype(auto) make_integer_array() {
   return make_integer_array_helper(std::make_index_sequence<N>());
}

inline static auto s_Outputs = make_integer_array<NUM_OUTPUTS>();