我简化了我的代码如下.
#includeclass NoncopyableItem { public: NoncopyableItem() { } NoncopyableItem(NoncopyableItem &&nt) { }; }; class Iterator { friend class Factory; public: ~Iterator() { } // weird private: Iterator() { } std::vector buffer_; }; class Factory { public: Iterator NewIterator() { return Iterator(); } }; int main() { Factory fa; auto it = fa.NewIterator(); return 0; }
我想在函数中利用RVO(返回值优化)NewIterator
,但是我收到以下错误:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/vector:62:0, from /cygdrive/c/Users/DELL/ClionProjects/destructor_test/main.cpp:1: /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = NoncopyableItem; _Args = {const NoncopyableItem&}]': /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_uninitialized.h:75:53: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator>; _ForwardIterator = NoncopyableItem*; bool _TrivialValueTypes = false]' /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_uninitialized.h:126:41: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator >; _ForwardIterator = NoncopyableItem*]' /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_uninitialized.h:279:63: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator >; _ForwardIterator = NoncopyableItem*; _Tp = NoncopyableItem]' /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_vector.h:324:32: required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = NoncopyableItem; _Alloc = std::allocator ]' /cygdrive/c/Users/DELL/ClionProjects/destructor_test/main.cpp:7:7: required from here /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/stl_construct.h:75:7: error: use of deleted function 'constexpr NoncopyableItem::NoncopyableItem(const NoncopyableItem&)' { ::new(static_cast (__p)) _T1(std::forward<_Args>(__args)...); } ^ /cygdrive/c/Users/DELL/ClionProjects/destructor_test/main.cpp:2:7: note: 'constexpr NoncopyableItem::NoncopyableItem(const NoncopyableItem&)' is implicitly declared as deleted because 'NoncopyableItem' declares a move constructor or move assignment operator class NoncopyableItem { ^ CMakeFiles/destructor_test.dir/build.make:62: recipe for target 'CMakeFiles/destructor_test.dir/main.cpp.o' failed
根据cppreference.com,NewIterator()
应符合RVO的要求.但是,似乎编译器试图调用默认的复制构造函数Iterator
,然后因为Iterator.buffer_
不可复制而失败.
好吧,令我惊讶的是,如果我删除Iterator
L#13中的析构函数,代码工作正常.
为什么析构函数会影响编译器的RVO行为?
首先,在这种情况下忘记RVO.这是一种合法的优化,但即使它确实发生了,代码必须是合法的,没有它.
所以考虑到这一点,我们来看看
auto it = fa.NewIterator();
该行尝试Iterator
从临时构造一个新的Iterator
.为此,我们需要以下两个中的任何一个†:
Iterator(const Iterator&); //or Iterator(Iterator&&);
现在在您发布的代码中,尝试使用隐式声明Iterator(const Iterator&);
将导致您显示的编译器错误,因为非静态成员的复制构造函数buffer_
无法编译.
未生成第二个候选项,因为Iterator
具有用户定义的析构函数.
如果删除用户定义的析构函数,编译器将生成移动构造函数Iterator(Iterator&&);
并在我们从临时构造时使用它.或者它可能不会和RVO相反,但它可以使用它,这是重要的部分.
†或者其他一些用户声明的构造函数当然使该行合法.但上面是两个常见的编译器生成的,你显然要问的.