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

前端 未结 5 1065
感情败类
感情败类 2020-11-22 08:30

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

#include 
#include <         


        
5条回答
  •  长情又很酷
    2020-11-22 08:44

    The synopsis of 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
    

提交回复
热议问题