c++ - 为什么下面的代码不能用 gcc 编译但用 clang 编译得很好

标签 c++ c++11 language-lawyer move-constructor noexcept

下面的代码可以用 clang 编译,但不能用 gcc 编译,请问这是 gcc 中的错误吗?

它只是一个包含 unique_ptr vector 和 std::function 作为成员的类,当我创建此类的 vector 时,我不能对此说保留或调整大小。 push_back 与 std::move 一起工作正常,而这只发生在 gcc 而不是 clang 上。

#include <algorithm>                                                                                                                                               
#include <memory>                                                                                                                                                          
#include <utility>                                                                                                                                                         
#include <iostream>  
#include <vector>
#include <functional>

using namespace std;                                                                                                                                                       

class ABC                                                                                                                                                                  
{                                                                                                                                                                          
public:                                                                                                                                                                    
    ABC()                                                                                                                                                                  
          {}                                                                                                                                                               
  private:                                                                                                                                                                 
    std::vector<std::unique_ptr<int>> up;                                                                                                                                  
    std::function<void (int*)> func;                                                                                                                                       
};                                                                                                                                                                         

int main()                                                                                                                                                                 
{                                                                                                                                                                          
    ABC a;                                                                                                                                                                 
    std::vector<ABC> vec;                                                                                                                                                  
    vec.reserve(1);                                                                                                                                                        
}    

gcc 的错误信息如下所示

In file included from /opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_tempbuf.h:60:0,
                 from /opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:62,
                 from /opt/wandbox/gcc-7.1.0/include/c++/7.1.0/algorithm:62,
                 from prog.cc:1:
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<int>; _Args = {const std::unique_ptr<int, std::default_delete<int> >&}]':
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:83:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >; _ForwardIterator = std::unique_ptr<int>*; bool _TrivialValueTypes = false]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:134:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >; _ForwardIterator = std::unique_ptr<int>*]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:289:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >; _ForwardIterator = std::unique_ptr<int>*; _Tp = std::unique_ptr<int>]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_vector.h:331:31:   required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<int>; _Alloc = std::allocator<std::unique_ptr<int> >]'
prog.cc:10:7:   required from 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = ABC; _Args = {const ABC&}]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:83:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const ABC*; _ForwardIterator = ABC*; bool _TrivialValueTypes = false]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:134:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const ABC*; _ForwardIterator = ABC*]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_uninitialized.h:289:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const ABC*; _ForwardIterator = ABC*; _Tp = ABC]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_vector.h:1263:35:   required from 'std::vector<_Tp, _Alloc>::pointer std::vector<_Tp, _Alloc>::_M_allocate_and_copy(std::vector<_Tp, _Alloc>::size_type, _ForwardIterator, _ForwardIterator) [with _ForwardIterator = const ABC*; _Tp = ABC; _Alloc = std::allocator<ABC>; std::vector<_Tp, _Alloc>::pointer = ABC*; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/vector.tcc:73:40:   required from 'void std::vector<_Tp, _Alloc>::reserve(std::vector<_Tp, _Alloc>::size_type) [with _Tp = ABC; _Alloc = std::allocator<ABC>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
prog.cc:24:18:   required from here
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/stl_construct.h:75:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/wandbox/gcc-7.1.0/include/c++/7.1.0/memory:80:0,
                 from prog.cc:2:
/opt/wandbox/gcc-7.1.0/include/c++/7.1.0/bits/unique_ptr.h:388:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^~~~~~~~~~

最佳答案

发生这种情况是因为 std::function 的 move ctor 不是 noexcept,但是 std::vector 只能使用 move ctor 如果它是 noexcept(强异常保证)。

问题是 std::unique_ptr 是(显然)不可复制的,所以这使得 ABC 作为一个整体不可复制。 要使 ABC noexcept 隐式可移动,它需要它的每个成员也是 noexcept - 可移动的。

如果删除 std::function,就会发生这种情况:.resize() 不需要复制 A.up(std::vector 的移动运算符是 noexcept),所以 std::unique_ptr(在 up) 可以移动,一切正常。

参见 this coliru : 如果您评论 noexceptvec.reserve() 将需要复制所有内容,问题又回来了。

添加 ABC(ABC&&) = default 解决了这个问题,因为用户声明的(虽然 defaulted)移动构造函数禁止复制构造函数的生成(参见 here ) .

我们也可以反其道而行之,在coliru中手动删除A的复制构造函数(并保留move ctor not noexcept ): here .


要解决这个问题,您可以std::function 存储在 std::unique_ptr 中,它有一个 nothrow 移动构造函数。 See here

关于c++ - 为什么下面的代码不能用 gcc 编译但用 clang 编译得很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44428191/

相关文章:

c++ - 如何避免 "' =': truncation of constant value"的警告消息?

c++ - 如果在推演过程中无法解析函数的地址,是SFINAE还是编译器错误?

c++ - 如何检测一个方法是否是虚拟的?

c++ - 将用户定义的模板类的实例传递给非模板函数

c++ - std::array 的大小是否由标准定义

c# - 使用点符号、orderBy 和 firstOrDefault 的开源 C++ LINQ 库?

c++ - 为什么我应该使用 CString 的 GetBuffer 成员而不是 SetAt?

C++11 对 lambda 返回类型的限制

c++ - Python 等效代码直接附加两个整数,就像在 C++ 中一样

c - 是否保证执行 memcpy(0,0,0) 是安全的?