C++ 使用std::move将唯一的_ptr移动到向量中

C++ 使用std::move将唯一的_ptr移动到向量中,c++,c++11,vector,std,unique-ptr,C++,C++11,Vector,Std,Unique Ptr,我一直在尝试创建一个唯一的\u ptr,然后用push\u back将其移动到该指针的向量中。当我尝试这样做时,我会得到一个很长的编译错误。我已经阅读了关于这个主题的多个问题,包括关于unique_ptr容器的部分: 还有这个问题: 这是一个不会编译的小示例程序: //ptrtest.cpp 包括 包括 类TestObject{ 公众: TestObjectent数据:datadata{} int getData{return data;} 私人: int数据; }; 使用名称空间std; in

我一直在尝试创建一个唯一的\u ptr,然后用push\u back将其移动到该指针的向量中。当我尝试这样做时,我会得到一个很长的编译错误。我已经阅读了关于这个主题的多个问题,包括关于unique_ptr容器的部分:

还有这个问题:

这是一个不会编译的小示例程序:

//ptrtest.cpp 包括 包括 类TestObject{ 公众: TestObjectent数据:datadata{} int getData{return data;} 私人: int数据; }; 使用名称空间std; int main int argc,char*argv[]{ 向量v; unique_ptr obj=unique_ptrnew TestObject5; v、 推回移动对象; 返回0; } 这是程序给我的编译器错误:

$ clang++ ptrtest.cpp -otest
In file included from ptrtest.cpp:1:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1574:36: error: 
      no matching constructor for initialization of
      'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
                ::new ((void*)__p) _Tp(__a0);
                                   ^   ~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1593:25: note: in
      instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > >
      >::construct<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >, std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > >' requested here
        __alloc_traits::construct(this->__alloc(),
                        ^
ptrtest.cpp:16:4: note: in instantiation of member function
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::push_back' requested here
        v.push_back(move(obj));
          ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2482:3: note: 
      candidate constructor not viable: 1st argument ('const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >')
      would lose const qualifier
  unique_ptr(unique_ptr&);
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2498:3: note: 
      candidate constructor not viable: no known conversion from 'const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
      to 'std::__1::nullptr_t' for 1st argument
  unique_ptr(nullptr_t) : __ptr_(pointer())
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2504:12: note: 
      candidate constructor not viable: no known conversion from 'const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
      to 'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject>
      >::pointer' (aka 'TestObject *') for 1st argument
  explicit unique_ptr(pointer __p)
           ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2516:3: note: 
      candidate constructor not viable: no known conversion from 'const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
      to '__rv<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > >' for 1st argument
  unique_ptr(__rv<unique_ptr> __u)
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2483:35: note: 
      candidate template ignored: deduced type 'unique_ptr<...>' of 1st
      parameter does not match adjusted type 'const unique_ptr<...>' of
      argument [with _Up = TestObject, _Ep =
      std::__1::default_delete<TestObject>]
  template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
                                  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2490:3: note: 
      candidate constructor not viable: requires 0 arguments, but 1 was provided
  unique_ptr() : __ptr_(pointer())
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2535:3: note: 
      candidate constructor not viable: requires 2 arguments, but 1 was provided
  unique_ptr(pointer __p, deleter_type __d)
  ^
In file included from ptrtest.cpp:2:
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1580:21: error: 
      no matching member function for call to 'construct'
    __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), ...
    ~~~~~~~~~~~~~~~~^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1599:9: note: in
      instantiation of function template specialization
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::__push_back_slow_path<const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> > >'
      requested here
        __push_back_slow_path(__x);
        ^
ptrtest.cpp:16:4: note: in instantiation of member function
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::push_back' requested here
        v.push_back(move(obj));
          ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note: 
      candidate template ignored: substitution failure [with _Tp =
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
      _A0 = std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >]
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note: 
      candidate function template not viable: requires 2 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note: 
      candidate function template not viable: requires 4 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note: 
      candidate function template not viable: requires 5 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error: 
      use of undeclared identifier 'construct'
                construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
                ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:898:21: note: in
      instantiation of function template specialization
      'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > >
      >::__construct_backward<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > *>' requested here
    __alloc_traits::__construct_backward(this->__alloc(), this->__begin_...
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1582:5: note: in
      instantiation of member function
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::__swap_out_circular_buffer'
      requested here
    __swap_out_circular_buffer(__v);
    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1599:9: note: in
      instantiation of function template specialization
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::__push_back_slow_path<const
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> > >'
      requested here
        __push_back_slow_path(__x);
        ^
ptrtest.cpp:16:4: note: in instantiation of member function
      'std::__1::vector<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >,
      std::__1::allocator<std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> > > >::push_back' requested here
        v.push_back(move(obj));
          ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note: 
      must qualify identifier to find this declaration in dependent base class
        static void construct(allocator_type&, _Tp* __p)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note: 
      must qualify identifier to find this declaration in dependent base class
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note: 
      must qualify identifier to find this declaration in dependent base class
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note: 
      must qualify identifier to find this declaration in dependent base class
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error: 
      no matching function for call to 'construct'
                construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
                ^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note: 
      candidate template ignored: substitution failure [with _Tp =
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
      _A0 = std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >]
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note: 
      candidate function template not viable: requires 2 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note: 
      candidate function template not viable: requires 4 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note: 
      candidate function template not viable: requires 5 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error: 
      no matching function for call to 'construct'
                construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
                ^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note: 
      candidate template ignored: substitution failure [with _Tp =
      std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
      _A0 = std::__1::unique_ptr<TestObject,
      std::__1::default_delete<TestObject> >]
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note: 
      candidate function template not viable: requires 4 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note: 
      candidate function template not viable: requires 5 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
                    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note: 
      candidate function template not viable: requires 2 arguments, but 3 were
      provided
        static void construct(allocator_type&, _Tp* __p)
                    ^
5 errors generated.

我的编译器是clang++并且我正在运行macOS 10.14.3。

你确定你在使用c++11吗

在c++11之前,push_back的签名。。。是:

void push_back (const value_type& val);
因此,即使您给出一个临时值,它仍将使用复制构造函数

在C++11中,有一个重载来处理使用临时变量来移动而不是复制


无论哪种方式,你都应该考虑使用。这是专门为处理临时对象和内联构造对象而设计的,因此没有副本

您确定正在使用c++11吗

在c++11之前,push_back的签名。。。是:

void push_back (const value_type& val);
因此,即使您给出一个临时值,它仍将使用复制构造函数

在C++11中,有一个重载来处理使用临时变量来移动而不是复制


无论哪种方式,你都应该考虑使用。这是专门为处理临时对象和内联构造对象而设计的,因此没有副本

哪个版本的叮当声?我看到它编译好了。我在任何版本的clang中都可以看到这个头的存在。你能详细介绍一下你的系统编译器版本,标准库,等等?$clang-version Apple LLVM version 10.0.0 clang-1000.10.44.4目标:x86_64-Apple-darwin18.0.0线程模型:posix InstalledDir:/Library/Developer/CommandLineTools/usr/binTry使用clang++-std=c++11-otest ptrtest.cpp编译我还建议使用-Wall-Wextra编译以获得有用的警告。从长远来看,它会帮你省去很多麻烦。哪种版本的叮当声?我看到它编译好了。我在任何版本的clang中都可以看到这个头的存在。你能详细介绍一下你的系统编译器版本,标准库,等等?$clang-version Apple LLVM version 10.0.0 clang-1000.10.44.4目标:x86_64-Apple-darwin18.0.0线程模型:posix InstalledDir:/Library/Developer/CommandLineTools/usr/binTry使用clang++-std=c++11-otest ptrtest.cpp编译我还建议使用-Wall-Wextra编译以获得有用的警告。从长远来看,这将为您节省很多麻烦。谢谢,默认情况下,我的编译器没有使用c++11。我在编译时添加了-std=c++11,它编译时没有错误。顺便说一句,unique_ptr的使用也应该在c++11之前出错。@Jarod42这就是为什么我感到困惑的原因,因为我已经成功地使用unique_ptr一段时间了,这让我觉得它已经作为c++11编译了。似乎编译器正在链接到的旧版本不允许我移动指针。谢谢,默认情况下,我的编译器没有使用c++11。我在编译时添加了-std=c++11,它编译时没有错误。顺便说一句,unique_ptr的使用也应该在c++11之前出错。@Jarod42这就是为什么我感到困惑的原因,因为我已经成功地使用unique_ptr一段时间了,这让我觉得它已经作为c++11编译了。似乎编译器正在链接到的旧版本不允许我移动指针。