Can I list-initialize a vector of move-only type?

Edit: Since @Johannes doesn’t seem to want to post the best solution as an answer, I’ll just do it. #include <iterator> #include <vector> #include <memory> int main(){ using move_only = std::unique_ptr<int>; move_only init[] = { move_only(), move_only(), move_only() }; std::vector<move_only> v{std::make_move_iterator(std::begin(init)), std::make_move_iterator(std::end(init))}; } The iterators returned by std::make_move_iterator will move the pointed-to element when being … Read more

Initializing a member array in constructor initializer

How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Yes. It’s using a struct that contains an array. You say you already know about that, but then I don’t understand the question. That way, you do initialize … Read more

C compile error: “Variable-sized object may not be initialized”

I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length … Read more