Initialize object containing C-style array as member variable (C++) Initialize object containing C-style array as member variable (C++) arrays arrays

Initialize object containing C-style array as member variable (C++)


Since Color is an aggregate you can use aggregate initialization and put the array initializer directly in the braces like

Color c = {10, {2, 6, 9}};

If you have to initialize c with an array, since it is small, you can just unroll it like

Color c = {10, {myRGB[0], myRGB[1], myRGB[2]}};


Supposing that there is a need to use an intermediate array, here is how one can do it:

#include <utility>#include <cstddef>struct Color  //this struct can't be modified{    double grey;    double rgb[3];};template<size_t N, size_t... IX>auto make_c_impl(double grey, double (&rgb)[N], std::index_sequence<IX...>) {    static_assert(sizeof(rgb) == sizeof(Color::rgb), "Arrays sizes must match!");    return Color{grey, {rgb[IX]...}};}template<size_t N>auto make_c(double grey, double (&rgb)[N]) {    return make_c_impl(grey, rgb, std::make_index_sequence<N>{});}double myRGB[3] = {2, 6, 9};Color c = make_c(10, myRGB); //this line now works

Note, that this code will not actually produce any unnecessary copying with any level of optimization.


As complement to others answers, the error is because in arrays are not copyable and trying to initialize an array from an lvalue has the semantic of invoking the copy-constructor, the same as:

double r1[3] = {0., 0., 0.};double r2[3] {r1} // doesn't compile

Your options are:

  • do list-initialization as @NathanOliver did
  • or expand the elements of the array to form a list-initialization as in the @SergeyA answer.