当元素类型没有默认构造函数时,有没有办法构造带有所有重复元素的模板化元素数组?
我尝试了以下内容:
template<typename T, int n> struct Array {
template<typename... Args> explicit Array(const T& arg1, Args... args)
: m_a{arg1, args...} { }
static Array<T,n> all(const T& value) {
Array<T,n> ar; // error: use of deleted function 'Array<TypeWithoutDefault, 10>::Array()'
for (int i=0; i<n; i++)
ar.m_a[i] = value;
return ar;
}
T m_a[n];
};
struct TypeWithoutDefault {
TypeWithoutDefault(int i) : m_i(i) { }
int m_i;
};
int main() {
// works fine
Array<TypeWithoutDefault,2> ar1 { TypeWithoutDefault{1}, TypeWithoutDefault{2} };
(void)ar1;
// I want to construct an Array of n elements all with the same value.
// However, the elements do not have a default constructor.
Array<TypeWithoutDefault,10> ar2 = Array<TypeWithoutDefault, 10>::all(TypeWithoutDefault(1));
(void)ar2;
return 0;
}
答案 0 :(得分:1)
以下将解决您的问题:
#if 1 // Not in C++11
template <std::size_t ...> struct index_sequence {};
template <std::size_t I, std::size_t ...Is>
struct make_index_sequence : make_index_sequence<I - 1, I - 1, Is...> {};
template <std::size_t ... Is>
struct make_index_sequence<0, Is...> : index_sequence<Is...> {};
#endif
namespace detail
{
template <typename T, std::size_t ... Is>
constexpr std::array<T, sizeof...(Is)> create_array(T value, index_sequence<Is...>)
{
// cast Is to void to remove the warning: unused value
return {{(static_cast<void>(Is), value)...}};
}
}
template <std::size_t N, typename T>
constexpr std::array<T, N> create_array(const T& value)
{
return detail::create_array(value, make_index_sequence<N>());
}
所以测试一下:
struct TypeWithoutDefault {
TypeWithoutDefault(int i) : m_i(i) { }
int m_i;
};
int main()
{
auto ar1 = create_array<10>(TypeWithoutDefault(42));
std::array<TypeWithoutDefault, 10> ar2 = create_array<10>(TypeWithoutDefault(42));
return 0;
}