hovnatan
hovnatan

Reputation: 1385

initializing a const array in the constructor

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)?

Upvotes: 4

Views: 867

Answers (3)

Jarod42
Jarod42

Reputation: 218323

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]...}
    {}
};

Upvotes: 3

user2249683
user2249683

Reputation:

You might use a std::array internally and convert an array:

#include <array>
#include <utility>

template <typename T, std::size_t ... I>
constexpr std::array<T, sizeof...(I)>
make_sequence(std::integer_sequence<std::size_t, I...>, const T (&array)[sizeof...(I)])  {
    return { array[I] ... };
}

template <typename T, std::size_t N>
constexpr std::array<T, N>
make_sequence(const T (&array)[N]) {
    return make_sequence(std::make_index_sequence<N>(), array);
}


// Test

#include <cassert>
struct A {
    constexpr A(const int (&arr)[5])
      :  arr_(make_sequence(arr))
    {}

    const std::array<int, 5> arr_;
};

int main()
{
    int array[5] = { 0, 1, 2, 3, 4 };
    A a(array);
    assert(a.arr_[2] == 2);
    return 0;
}

However, if you are free to modify the interface of class A, go with the answer of @ecatmur.

Upvotes: 1

ecatmur
ecatmur

Reputation: 157504

Use std::array:

struct A {
    A(std::array<int, 5> const& arr)
      :  arr_(arr)
    {}

    std::array<int, 5> const arr_;
}

Upvotes: 5

Related Questions