Search code examples
c++arraysinitializer-list

std::initializer list from already existing std::array without enumerating each element


Lets assume that I have a class with the following constructor:

class Foo {
    Foo(std::initializer_list<uin8_t> args)
    {
     ....
    }
}

and I have the following array: std::array<uint8_t, 6> bar.

Now I would like to create an object off foo with the bar array. Is there an other way as doing it in the follwing way:

Foo f (bar[0], bar[1], bar[2], bar[3], bar[4], bar[5]);

This way seems a bit complicated and it feels like this is not the way it should be.

So, can I create a std::initializer list from an already existing array, without enumerating each array element?


Solution

  • No, you can't do it. This API which only accepts an initializer_list and has no facility to accept, say, a pair of iterators, or a pointer plus a size, is deficient. You'll virtually never see an API like this in the wild.