what is the correct and easy way for initialization of class containing array of tuples?

Viewed 40

I have a class which contains array of tuples like the following:

template<size_t __v, typename ... __tz>
class turray{
public:
    std::tuple<__tz ...> array[__v];
};

It does not have any user defined constructor and I was wondering how can I initialize it. Please consider the following methods:

int main(){
    turray<2, int, float> mturray0{std::tuple<int, float>{1, 1.1}, std::tuple<int, float>{2, 2.2}}; //works but is very big
    turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};// causes error
}

The first method works but is very big and not desired. the second method causes the following error:

error: too many initializers for ‘turray<2, int, float>’
  227 |  turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};
      |                                                   ^

I would appreciate it if some one could show me what should be the correct method.

1 Answers

You just need to add another pair of braces:

turray<2, int, float> mturray1 { { {1, 1.1}, {2, 2.2} } };
//                                 ^      ^                tuple
//                               ^                    ^    array
//                             ^                        ^  turray

Here's a demo.

Related