簡體   English   中英

被矢量擊敗<unique_ptr>

[英]Defeated by vector<unique_ptr>

多年后回到C ++; 試圖趕上C ++ 11和14.我已經閱讀了關於rvalues和移動語義的內容。 我以為我理解了這個概念。 顯然不是。 我看過幾十個例子。 但我根本無法編譯我的代碼。 我必須遺漏一些明顯的例子。 我總是得到關於copy ctor被刪除的錯誤,因為unique_ptr<int>有一個用戶聲明的移動ctor。 顯然我對這個概念缺少了一些東西,但我無法弄清楚它是什么。 這是代碼,根據其本質:

#include <memory>
#include <utility>
#include <vector>

int main(int, char*[]) {
  auto oneInt{std::make_unique<int>(0)};
  auto someInts{std::vector<std::unique_ptr<int>>{std::move(oneInt)}};

  return 0;
}

我究竟做錯了什么?

編輯:這是這個特定代碼的錯誤。 請注意,我已經嘗試了我能想到的代碼的每個變體,結果各不相同,但基本問題始終是相同的:復制ctor已刪除,因為unique_ptr<int>具有用戶聲明的移動ctor。

編輯:我已將代碼更新為#include <memory> ,並粘貼了新錯誤。 我只能希望問題是那樣的傻事。

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1752:31: error: call to implicitly-deleted copy constructor of
      'std::__1::unique_ptr<int, std::__1::default_delete<int> >'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1668:18: note: in instantiation of function template
      specialization 'std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > >::construct<std::__1::unique_ptr<int, std::__1::default_delete<int> >,
      const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1514:14: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::__construct<std::__1::unique_ptr<int,
      std::__1::default_delete<int> >, const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1598:17: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::construct<std::__1::unique_ptr<int,
      std::__1::default_delete<int> >, const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
                construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
                ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1024:21: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::__construct_range_forward<const
      std::__1::unique_ptr<int, std::__1::default_delete<int> > *, std::__1::unique_ptr<int, std::__1::default_delete<int> > *>' requested here
    __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1285:9: note: in instantiation of function template
      specialization 'std::__1::vector<std::__1::unique_ptr<int, std::__1::default_delete<int> >, std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> >
      > >::__construct_at_end<const std::__1::unique_ptr<int, std::__1::default_delete<int> > *>' requested here
        __construct_at_end(__il.begin(), __il.end(), __il.size());
        ^
virtual.cpp:7:21: note: in instantiation of member function 'std::__1::vector<std::__1::unique_ptr<int, std::__1::default_delete<int> >,
      std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::vector' requested here
      auto someInts{std::vector<std::unique_ptr<int>>{std::move(oneInt)}};
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2621:31: note: copy constructor is implicitly deleted because
      'unique_ptr<int, std::__1::default_delete<int> >' has a user-declared move constructor
    _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT

您的代碼存在很多問題。 首先,過度使用{}初始化會混淆事物。 使用auto很好,但是auto x{...}; 聲明充滿了危險,因為auto x{single_value}的含義隨着時間的推移而變化。 最好使用auto x = single_value; 語法哪里合理。

其次,您不能通過{}初始化列表將unique_ptr插入到容器中。 完全沒有。 通過std::initializer_list 必須是可復制的,而unique_ptr則不是。

你想要的是這個:

auto oneInt = std::make_unique<int>(0);
std::vector<std::unique_ptr<int>> someInts;
someInts.push_back(std::move(oneInt));

簡短的回答是std::initializer_list包含值; 並且它們不能被移出,只能從中復制出來。 需要明確的是,如果對vector使用initializer_list構造形式,則列表中的項目將復制到向量中。

請參閱此主題以討論您的確切問題和一些建議的解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM