1

I am trying to initialize a struct that has a std::array member:

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

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(init_type array) : array_{array}  {}

    foo_type array_;
};

int main() {
    Foo foo {{1,2}};
    //Foo foo1 {{{1,2}}};

    //std::unique_ptr<Foo> foo2 = std::make_unique<Foo>({{1,2}});

    return 0;
}

This gives me error:

test.cpp: In constructor ‘Foo::Foo(Foo::init_type)’:
test.cpp:8:35: error: cannot convert ‘Foo::init_type’ {aka ‘std::initializer_list<int>’} to ‘int’ in initialization
    8 |     Foo(init_type array) : array_{array}  {}
      |                                   ^~~~~
      |                                   |
      |                                   Foo::init_type {aka std::initializer_list<int>}

What am I missing here?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174

0 Answers0