0

I can initialize a struct with a single std::array element using a variadic template constructor:

#include <array>
#include <initializer_list>
#include <memory>

struct Foo {
    using data_type = int;
    using foo_type = std::array<data_type,2>;
    using init_type = std::initializer_list<data_type>;
    template <typename... T> Foo(T... array) : array_{array...}  {}
    foo_type array_;
};

int main() {
    Foo foo {1,2};  // this works
    std::unique_ptr<Foo> foo2 = std::make_unique<Foo>(3,4); // this also works!
    return 0;
}

This works fine! But how can I do the initialization if I have more than a single array member in Foo ? I tried the following which does not work:

#include <array>
#include <initializer_list>
#include <memory>

struct Foo {
    using data_type = int;
    using foo_type = std::array<data_type,2>;
    using init_type = std::initializer_list<data_type>;
    template <typename T> Foo(T array1, T array2)
        : array1_{init_array(array1)}, array2_{init_array(array2)}   {}
    template <typename... T> constexpr auto init_array(T... array) {
        return array...;
    }

    foo_type array1_, array2_;
};

int main() {
    Foo foo {{1,2},{3,4}};

    return 0;
}

This gives an error:

test.cpp: In member function ‘constexpr auto Foo::init_array(T ...)’:
test.cpp:12:16: error: parameter packs not expanded with ‘...’:
   12 |         return array...;
      |                ^~~~~
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • What is `init_array` doing there? `template Foo` Does it have to be a template? It's going to be `foo_type` anyway. – KamilCuk Sep 06 '22 at 09:54
  • See dupe and this answer that uses `std::array` parameter: [How do I initialize a member array with an initializer_list?](https://stackoverflow.com/a/5550515/12002570) – Jason Sep 06 '22 at 10:00
  • @JasonLiam the accepted answer there is not applicable here. – 463035818_is_not_an_ai Sep 06 '22 at 10:00
  • @463035818_is_not_a_number So what? There is no requirement that for dupe the answer must be the accepted one. Did you even read all the answers there. [This answer](https://stackoverflow.com/a/5550515/12002570) answers OP's question. – Jason Sep 06 '22 at 10:01
  • 1
    @463035818_is_not_a_number There was no need to reopen this question. OP's question is answered by [this answer](https://stackoverflow.com/a/5550515/12002570). Read all the answers before reopening questions without even thinking. – Jason Sep 06 '22 at 10:04
  • 1
    @463035818_is_not_a_number Additionally OP already asked the same question before: https://stackoverflow.com/questions/73619430/how-to-initialize-a-struct-with-a-stdarray-member. Just having two array members instead of one doesn't make this a new question. Tomorrow, OP might ask the same question for 3 array members then day after tomorrow for 4 array members and so on. That doesn't make them a new question. There already exists 2 dupes for this question. – Jason Sep 06 '22 at 10:06

1 Answers1

0

Just specify the parameters as arrays themselves:

struct Foo {
    using data_type = int;
    using foo_type = std::array<data_type,2>;
    using init_type = std::initializer_list<data_type>;
    
    Foo(const foo_type &arr, const foo_type &arr2): array1_{ arr }, array2_{ arr2 } {}

    foo_type array1_, array2_;
};
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • Dupe of this answer: https://stackoverflow.com/a/5550515/12002570 – Jason Sep 06 '22 at 10:02
  • This works fine with this constructor: `Foo foo {{1,2},{3,4}}` but if I try to create a unique pointer it does not work: `std::make_unique({3,4}, {5,6})`. Any ideas? – Håkon Hægland Sep 06 '22 at 10:05
  • 1
    @HåkonHægland templates don't know that you want `std::array` out of the braced initialiser list. In this case you have give this type explicitly: `std::make_unique( Foo::foo_type{ 3,4 }, Foo::foo_type{ 5,6 });` – The Dreams Wind Sep 06 '22 at 10:12
  • @JasonLiam you can reproach me for not pointing to the dupe, but I didn't need this answer to form mine. I used the names of types and classes from OP answer, and I have no idea why he used exactly the same names – The Dreams Wind Sep 06 '22 at 10:16