多年後回到C++;試圖追趕C++ 11 & 14.我讀過rvalues和移動語義。我以爲我理解了這個概念。顯然不是。我看了幾十個例子。但我根本無法讓我的代碼編譯。我必須在示例中錯過某些明顯的東西。由於unique_ptr<int>
有一個用戶聲明的移動ctor,我總是會得到有關複製ctor被刪除的錯誤。顯然我對這個概念缺少了一些東西,但我無法弄清楚它是什麼。這裏的代碼,精簡到它的本質:擊敗向量<unique_ptr>
#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
當問構建錯誤通常有助於看到的錯誤。您能否編輯您的問題以包含來自版本的完整,完整和未修改的輸出,包括文本和任何可能的信息說明? –
其中之一,你沒有包含''標題。 –
StoryTeller
'oneInt'被移到初始化列表中,但被複製出來。 – molbdnilo