I am trying to achieve something like this
struct A {
A(const int (&arr)[5])
: arr_(arr)
{}
const int arr_[5];
}
and obviously this doesn't work. My intent is to keep arr_
field constant. What is the best way to achieve this (can be C++11)?
Use std::array
:
struct A {
A(std::array<int, 5> const& arr)
: arr_(arr)
{}
std::array<int, 5> const arr_;
}
With forwarding constructor:
struct A {
A(const int (&arr)[5]) : A(arr, std::make_index_sequence<5>()) {}
const int arr_[5];
private:
template <std::size_t ... Is>
A(const int (&arr)[5], std::index_sequence<Is...>)
: arr_{arr[Is]...}
{}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With