为什么以下代码不使用GCC编译,而是用Clang编译罚款

Why the below code does not compile with gcc but compiles fine with clang

本文关键字:编译 Clang GCC 代码 为什么      更新时间:2023-10-16

下面的代码在clang上填充了罚款,但没有GCC,这是GCC中的错误吗?

中的错误

它只是一个包含unique_ptr和std ::函数成员的向量的类,当我创建此类的向量时,我不能对此表示储备或调整大小。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的移动CTOR不是noexcept,但是std::vector只能在noexcept时使用MOVE CTOR(强大的例外保证(。

问题是std::unique_ptr是(显然(不可复制的,因此ABC总体上不可复制。要使ABC noexcept-隐式可移动,它也需要其每个成员都可以 noexcept-可移动。

如果您删除了std::function,那就是发生的事情:.resize()不需要复制A.up(std::vector的移动操作员是noexcept(,因此std::unique_ptr S(内部up(只能移动,并且一切正常,一切正常。

请参阅此coliru :如果评论noexcept,则vec.reserve()需要复制所有内容,并且问题又回来了。

添加 ABC(ABC&&) = default解决了问题,因为用户 - 指定(尽管default ED(移动CTOR会抑制复制构造函数的生成(请参阅此处(。

我们也可以采取相反的方式,并在Coliru中手动删除A的复制构造函数(并保持移动ctor not noexcept(:此处


要解决问题,您可以将std::function存储在具有nothrow MOVE CTOR的std::unique_ptr中。请参阅此处

i 以为 gcc对构造函数很挑剔;它阻止了Move-Ctor 1 的生成。明确提供它们后,它停止抱怨:

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

1 i think think (再次,我不确定(,因为 ABC的默认复制品被定义为 noexcept,而这是因为默认的副本copy-ctor,并且是如此优先在默认的移动过程中(由于我们在这里与不可仿制的成员打交道,但它仍然不明显(。尝试生成默认的 noexcept move-ctor结果:

main.cpp:14:4: note: 'ABC::ABC(ABC&&) noexcept' is implicitly deleted because its exception-specification does not match the implicit exception-specification ''
    ABC(ABC&&) noexcept = default;
    ^~~

因此,迫使无noexcept的生成允许选择它。为什么Clang对此没有问题 - 我不知道。

删除std::function允许生成默认的noexcept移动CTOR的事实暗示了上述解释。std::function没有noexcept移动CTOR(非常糟糕(,因此整个班级都落到了复印件上。由于unique_ptr没有一个,因此整个事情都会破裂。

gcc接受了std :: funct的移动ctor上缺少noexcept作为bug

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81017

// - - C - -

  /** 2227        *  @brief %Function move constructor. 2228        *  @param
 __x A %function object rvalue with identical call signature. 2229        * 2230        
*  The newly-created %function contains the target of @a __x 2231        *  (if 
it has one). 2232        */ 2233       
function(function&& __x) : _Function_base()  {     __x.swap(*this); }