以下代码无法编译,但我不确定为什么。我原以为编译器会在任何地方都使用移动构造函数,因为一切都是右值,但它似乎尝试使用复制构造函数。
#include <memory>
#include <list>
using ptr_t = std::unique_ptr< int >;
using list_t = std::list< ptr_t >;
list_t foo(void) {
return list_t {
ptr_t( new int( 42 ) )
};
}
int main( void ) {
list_t list = foo( );
return * ( list.front( ) );
}
错误:
In file included from x.cc:2:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/list:62:
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:114:33: error: call to deleted constructor of 'std::unique_ptr<int, std::default_delete<int> >'
: __detail::_List_node_base(), _M_data(std::forward<_Args>(__args)...)
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/ext/new_allocator.h:120:23: note: in instantiation of function template specialization 'std::_List_node<std::unique_ptr<int,
std::default_delete<int> > >::_List_node<const std::unique_ptr<int, std::default_delete<int> > &>' requested here
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:505:32: note: in instantiation of function template specialization
'__gnu_cxx::new_allocator<std::_List_node<std::unique_ptr<int, std::default_delete<int> > > >::construct<std::_List_node<std::unique_ptr<int, std::default_delete<int> > >, const std::unique_ptr<int,
std::default_delete<int> > &>' requested here
_M_get_Node_allocator().construct(__p,
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:1561:18: note: in instantiation of function template specialization 'std::list<std::unique_ptr<int, std::default_delete<int> >,
std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_create_node<const std::unique_ptr<int, std::default_delete<int> > &>' requested here
_Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:1026:17: note: in instantiation of function template specialization 'std::list<std::unique_ptr<int, std::default_delete<int> >,
std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_insert<const std::unique_ptr<int, std::default_delete<int> > &>' requested here
{ this->_M_insert(end(), std::forward<_Args>(__args)...); }
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:1491:6: note: in instantiation of function template specialization 'std::list<std::unique_ptr<int, std::default_delete<int> >,
std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::emplace_back<const std::unique_ptr<int, std::default_delete<int> > &>' requested here
emplace_back(*__first);
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/stl_list.h:608:9: note: in instantiation of function template specialization 'std::list<std::unique_ptr<int, std::default_delete<int> >,
std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_initialize_dispatch<const std::unique_ptr<int, std::default_delete<int> > *>' requested here
{ _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
^
x.cc:8:12: note: in instantiation of member function 'std::list<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::list' requested here
return list_t {
^
/usr/lib/gcc/i686-pc-linux-gnu/4.8.0/../../../../include/c++/4.8.0/bits/unique_ptr.h:273:7: note: function has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.