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

8

126

If I pass the following code through my GCC 4.7 snapshot, it tries to copy the unique_ptrs into the vector.

#include <vector>
#include <memory>

int main() {
    using move_only = std::unique_ptr<int>;
    std::vector<move_only> v { move_only(), move_only(), move_only() };
}

Obviously that cannot work because std::unique_ptr is not copyable:

error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete; std::unique_ptr<_Tp, _Dp> = std::unique_ptr]'

Is GCC correct in trying to copy the pointers from the initializer list?

Humbuggery answered 12/12, 2011 at 0:49 Comment(1)
Visual Studio and clang has the same behaviorDiocesan
B
55

The synopsis of <initializer_list> in 18.9 makes it reasonably clear that elements of an initializer list are always passed via const-reference. Unfortunately, there does not appear to be any way of using move-semantic in initializer list elements in the current revision of the language.

Specifically, we have:

typedef const E& reference;
typedef const E& const_reference;

typedef const E* iterator;
typedef const E* const_iterator;

const E* begin() const noexcept; // first element
const E* end() const noexcept; // one past the last element
Burgos answered 12/12, 2011 at 0:55 Comment(2)
Consider the in<T> idiom described on cpptruths (cpptruths.blogspot.com/2013/09/…). The idea is to determine lvalue/rvalue at run-time and then call move or copy-construction. in<T> will detect rvalue/lvalue even though the standard interface provided by initializer_list is const reference.Barm
@Barm Doesn't seem so "idiomatic to me": isn't it, instead, pure UB? as not just the iterator but rather the underlying elements themselves might be const, which cannot be cast away in a well-formed program.Elisavetgrad
B
85

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 dereferenced.


Original answer: We're gonna utilize a little helper type here:

#include <utility>
#include <type_traits>

template<class T>
struct rref_wrapper
{ // CAUTION - very volatile, use with care
  explicit rref_wrapper(T&& v)
    : _val(std::move(v)) {}

  explicit operator T() const{
    return T{ std::move(_val) };
  }

private:
  T&& _val;
};

// only usable on temporaries
template<class T>
typename std::enable_if<
  !std::is_lvalue_reference<T>::value,
  rref_wrapper<T>
>::type rref(T&& v){
  return rref_wrapper<T>(std::move(v));
}

// lvalue reference can go away
template<class T>
void rref(T&) = delete;

Sadly, the straight-forward code here won't work:

std::vector<move_only> v{ rref(move_only()), rref(move_only()), rref(move_only()) };

Since the standard, for whatever reason, doesn't define a converting copy constructor like this:

// in class initializer_list
template<class U>
initializer_list(initializer_list<U> const& other);

The initializer_list<rref_wrapper<move_only>> created by the brace-init-list ({...}) won't convert to the initializer_list<move_only> that the vector<move_only> takes. So we need a two-step initialization here:

std::initializer_list<rref_wrapper<move_only>> il{ rref(move_only()),
                                                   rref(move_only()),
                                                   rref(move_only()) };
std::vector<move_only> v(il.begin(), il.end());
Black answered 12/12, 2011 at 1:30 Comment(12)
Ah... this is the rvalue analogue of std::ref, non? Maybe it should be called std::rref.Burgos
IIRC you need an explicitly defined copy constructor in move_only, because the spec says that the copy constructor is defined as deleted if the class contains an rvalue reference member. EDIT: Ah NVM, rref_wrapper<T> is never copied in your case so it doesn't matter.Severn
Now, I guess this should not be left without being mentioned in a comment :) move_only m[] = { move_only(), move_only(), move_only() }; std::vector<move_only> v(std::make_move_iterator(m), std::make_move_iterator(m + 3));.Severn
@Johannes: Sometimes, it's the simple solutions that just elude me. Though I gotta admit, I didn't bother with those move_iterators yet.Black
@Johannes: Also, why isn't that an answer? :)Black
Is v.reserve(init.size()) done when using std::make_move_iterator? Not all iterators can provide the number of elements. I came up with a similar solution that just used vec.reserve(arr.size()); for (auto &v: arr) vec.emplace_back(std::move(v)).Domingadomingo
@JohanLundberg: I'd consider that a QoI issue, but I don't see why it couldn't do that. VC++'s stdlib for example tag-dispatches based on the iterator category and uses std::distance for forward-or-better iterators and std::move_iterator adapts the underlying iterator's category. Anyways, good and concise solution. Post it as an answer, maybe?Black
I would not have used a rvalue reference member, as you have to ensure that the reference must be consumed before the next sequence point is reached. It seems to me its not the case here, thus yielding undefined behavior. However, you could use a mutable non-reference member to get a similar behavior. Source: #4176828Paulie
@JBJansen Not before the next 'sequence point' (a concept that doesn't exist anymore anyways), but before the end of the full expression, which is the case here.Black
Can "best solution" from @Johannes be improved by moving most of the remaining horribleness into a helper function? template <typename MoveOnly, int N> std::vector<MoveOnly> VectorOfMoveOnlyFromInitializerList(std::array<MoveOnly, N> init) { return vector<MoveOnly>(std::make_move_iterator(std::begin(init)), std::make_move_iterator(std::end(init))); } Then use it as: std::vector<move_only> v = VectorOfMoveOnlyFromInitializerList<move_only,3>({{move_only(), move_only(), move_only()}}); Wish I didn't need the <move_only,3> in caller, but apparently it can't figure out the type without it.Silurian
See also my answer that uses make_array(). Can that be made more direct?Herakleion
I wish you would come out and just say: don't use a vector, use an array (since array initializers aren't broken). Which at least addresses the case of static initializers that you'd like to not leak into your otherwise meticulously valgrindable heap. The rest of the ugliness is just lipstick on a pig.Handiness
B
55

The synopsis of <initializer_list> in 18.9 makes it reasonably clear that elements of an initializer list are always passed via const-reference. Unfortunately, there does not appear to be any way of using move-semantic in initializer list elements in the current revision of the language.

Specifically, we have:

typedef const E& reference;
typedef const E& const_reference;

typedef const E* iterator;
typedef const E* const_iterator;

const E* begin() const noexcept; // first element
const E* end() const noexcept; // one past the last element
Burgos answered 12/12, 2011 at 0:55 Comment(2)
Consider the in<T> idiom described on cpptruths (cpptruths.blogspot.com/2013/09/…). The idea is to determine lvalue/rvalue at run-time and then call move or copy-construction. in<T> will detect rvalue/lvalue even though the standard interface provided by initializer_list is const reference.Barm
@Barm Doesn't seem so "idiomatic to me": isn't it, instead, pure UB? as not just the iterator but rather the underlying elements themselves might be const, which cannot be cast away in a well-formed program.Elisavetgrad
R
13

As mentioned in other answers, the behaviour of std::initializer_list is to hold objects by value and not allow moving out, so this is not possible. Here is one possible workaround, using a function call where the initializers are given as variadic arguments:

#include <vector>
#include <memory>

struct Foo
{
    std::unique_ptr<int> u;
    int x;
    Foo(int x = 0): x(x) {}
};

template<typename V>        // recursion-ender
void multi_emplace(std::vector<V> &vec) {}

template<typename V, typename T1, typename... Types>
void multi_emplace(std::vector<V> &vec, T1&& t1, Types&&... args)
{
    vec.emplace_back( std::move(t1) );
    multi_emplace(vec, args...);
}

int main()
{
    std::vector<Foo> foos;
    multi_emplace(foos, 1, 2, 3, 4, 5);
    multi_emplace(foos, Foo{}, Foo{});
}

Unfortunately multi_emplace(foos, {}); fails as it cannot deduce the type for {}, so for objects to be default-constructed you have to repeat the class name. (or use vector::resize)

Reporter answered 10/11, 2015 at 1:3 Comment(1)
The recursive pack expansion could be replaced by the dummy array comma operator hack , to save a couple of lines of codeReporter
H
11

Update for C++20: Using Johannes Schaub's trick of std::make_move_iterator() with C++20's std::to_array(), you can use a helper function like unto make_tuple() etc., here called make_vector():

#include <array>
#include <memory>
#include <vector>

struct X {};

template<class T, std::size_t N>
auto make_vector( std::array<T,N>&& a )
    -> std::vector<T>
{
    return { std::make_move_iterator(std::begin(a)),
             std::make_move_iterator(std::end(a)) };
}

template<class... T>
auto make_vector( T&& ... t )
{
    return make_vector( std::to_array({ std::forward<T>(t)... }) );
}

int main()
{
    using UX = std::unique_ptr<X>;
    const auto a  = std::to_array({ UX{}, UX{}, UX{} });     // Ok
    const auto v0 = make_vector( UX{}, UX{}, UX{} );         // Ok
    //const auto v2 = std::vector< UX >{ UX{}, UX{}, UX{} }; // !! Error !!
}

See it live on Godbolt.


Similar answer for older C++:

Using Johannes Schaub's trick of std::make_move_iterator() with std::experimental::make_array(), you can use a helper function:

#include <memory>
#include <type_traits>
#include <vector>
#include <experimental/array>

struct X {};

template<class T, std::size_t N>
auto make_vector( std::array<T,N>&& a )
    -> std::vector<T>
{
    return { std::make_move_iterator(std::begin(a)), std::make_move_iterator(std::end(a)) };
}

template<class... T>
auto make_vector( T&& ... t )
    -> std::vector<typename std::common_type<T...>::type>
{
    return make_vector( std::experimental::make_array( std::forward<T>(t)... ) );
}

int main()
{
    using UX = std::unique_ptr<X>;
    const auto a  = std::experimental::make_array( UX{}, UX{}, UX{} ); // Ok
    const auto v0 = make_vector( UX{}, UX{}, UX{} );                   // Ok
    //const auto v1 = std::vector< UX >{ UX{}, UX{}, UX{} };           // !! Error !!
}

See it live on Coliru.

Perhaps someone can leverage std::make_array()'s trickery to allow make_vector() to do its thing directly, but I did not see how (more accurately, I tried what I thought should work, failed, and moved on). In any case, the compiler should be able to inline the array to vector transformation, as Clang does with O2 on GodBolt.

Herakleion answered 20/3, 2017 at 22:31 Comment(0)
B
1

This is the solution I like the most.

C++17 version

#include <vector>
#include <memory>

template <typename T, typename ...Args>
std::vector<T> BuildVectorFromMoveOnlyObjects(Args&&... args) {
  std::vector<T> container;
  container.reserve(sizeof...(Args));
  ((container.emplace_back(std::forward<Args>(args))), ...);
  return container;
}


int main() {
  auto vec = BuildVectorFromMoveOnlyObjects<std::unique_ptr<int>>(
      std::make_unique<int>(10),
      std::make_unique<int>(50));
}

A bit uglier C++11 version

template <typename T, typename ...Args>
std::vector<T> BuildVectorFromMoveOnlyObjects(Args&&... args) {
  std::vector<T> container;

    using expander = int[];
    (void)expander{0, (void(container.emplace_back(std::forward<Args>(args))), 0)... };

  return container;
}
Bisset answered 13/7, 2022 at 20:3 Comment(0)
P
1

I've made a small library for this purpose.

run on gcc.godbolt.org

#include <better_braces.hpp>

#include <iostream>
#include <memory>
#include <vector>

int main()
{
    std::vector<std::unique_ptr<int>> foo = init{nullptr, std::make_unique<int>(42)};
    std::cout << foo.at(0) << '\n'; // 0
    std::cout << foo.at(1) << " -> " << *foo.at(1) << '\n'; // 0x602000000010 -> 42
}

Unlike the move_iterator approach, this doesn't necessarily move each element. nullptr is emplaced directly into the vector, without constructing an intermediate std::unique_ptr.

This allows it to work even with non-movable types:

std::vector<std::atomic_int> bar = init{1, 2, 3};
Privet answered 29/11, 2022 at 20:0 Comment(2)
Wow. this is really nice. After struggling with this all day, it's refreshing to see how simple it could be. Any reason you know of that this behavior couldn't make it into standard c++ some day?Silurian
@DonHatch I don't know. std::initializer_list just looks poorly designed to me.Privet
H
0

An attempt at a simple to-the-point answer for the rest of us.

You can't. It's broken.

Fortunately, array initializers aren't broken.

static std::unique_ptr<SerializerBase> X::x_serializers[] = { 
    std::unique_ptr<SerializerBase>{
        new Serializer<X,int>("m_int",&X::m_int)
    },
    std::unique_ptr<SerializerBase>{
        new Serializer<X,double>("m_double",&X::m_double)
    },
  nullptr, // lol. template solutions from hell possible here too.
};

If you then want to use that array to initialize a std::vector<std::unique_ptr<T>>, there are endless ways to do so, many of which involve baroquely unpleasant template metaprogramming, all of which can be avoided with a for loop.

Fortunately, using an array instead of a std::vector works in a lot of cases where you really would have preferred to use a std::vector.

Alternately, consider writing a custom::static_vector<T> class that take T*'s in an initializer list, and deletes them in its's destructor. Also not happy, but you need to resign yourself to the fact that std::vector<std::unique_ptr<T>> isn't going to work in reasonable time or with reasonable effort. You can just delete any methods that do a potential move (move and copy constructors,T&operator[]() &c). Or get fancy and implement rudimentary move semantics if you must (but you probably don't).

See [1] for a defense of this, provided for members of the Purist priesthood.


[1] Programming languages are supposed to increase productivity. Template meta-programming isn't doing that in this case. All I want is a way to ensure that I don't leak memory allocated in static initialization into the heap, thereby making it impossible to use valgrind to verify that I'm not leaking memory.

That's an everyday use-case. And it shouldn't be difficult. Making it remotely complicated only leads to shortcuts down the road.

Handiness answered 17/4, 2022 at 6:53 Comment(0)
M
-2

As it has been pointed out, it is not possible to initialize a vector of move-only type with an initializer list. The solution originally proposed by @Johannes works fine, but I have another idea... What if we don't create a temporary array and then move elements from there into the vector, but use placement new to initialize this array already in place of the vector's memory block?

Here's my function to initialize a vector of unique_ptr's using an argument pack:

#include <iostream>
#include <vector>
#include <make_unique.h>  /// @see https://mcmap.net/q/67534/-make_unique-and-perfect-forwarding

template <typename T, typename... Items>
inline std::vector<std::unique_ptr<T>> make_vector_of_unique(Items&&... items) {
    typedef std::unique_ptr<T> value_type;

    // Allocate memory for all items
    std::vector<value_type> result(sizeof...(Items));

    // Initialize the array in place of allocated memory
    new (result.data()) value_type[sizeof...(Items)] {
        make_unique<typename std::remove_reference<Items>::type>(std::forward<Items>(items))...
    };
    return result;
}

int main(int, char**)
{
    auto testVector = make_vector_of_unique<int>(1,2,3);
    for (auto const &item : testVector) {
        std::cout << *item << std::endl;
    }
}
Musculature answered 15/5, 2013 at 11:36 Comment(3)
That is a terrible idea. Placement new is not a hammer, it is a tool of fine precision. result.data() is not a pointer to some random memory. It is a pointer to an object. Think of what happens to that poor object when you placement new over it.Humbuggery
Additionally, the array form of placement new is not really usable #8720925Humbuggery
@R. Martinho Fernandes: thanks for pointing out that placement-new for arrays would not work. Now I see why that was a bad idea.Musculature

© 2022 - 2024 — McMap. All rights reserved.