Let's say I have a class without a default constructor called Foo<\/code> .
std::vector<Foo> vec(100, Foo(5));
std::array<\/code> is just a thin wrapper around a fixed array.
It has no repeat-insert logic, like
std::vector<\/code> does.
Since
Foo<\/code> does not have a default constructor, the only way to initialize an instance of
std::array<Foo, N><\/code> is to use aggregate initialization<\/a> with
N<\/code> number of values specified (sorry, I know you don't want to to do this), eg:
std::array<Foo, 100> arr{5, 5, 5, ...}; // N times...
With copy constructor, something along these lines:
template <typename T, size_t... Is>
std::array<T, sizeof...(Is)> MakeArrayHelper(
const T& val, std::index_sequence<Is...>) {
return {(Is, val), ...};
}
template <typename T, size_t N>
std::array<T, N> MakeArray(const T& val) {
return MakeArrayHelper<T>(val, std::make_index_sequence<N>{});
}
std::array<Foo, 100> arr = MakeArray<Foo, 100>(Foo(5));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.