为什么下面的代码不能用gcc编译,但用clang编译好

时间:2017-06-08 06:29:07

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

下面的代码用clang而不是gcc编译好,任何解释都是gcc中的错误吗?

它只是一个包含unique_ptr和std :: function的向量作为成员的类,当我创建这个类的向量时,我不能说保留或调整大小。使用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;
       ^~~~~~~~~~

3 个答案:

答案 0 :(得分:3)

认为 GCC对构造函数很挑剔;它会阻止move-ctor 1 的生成。在您明确提供后,它会停止抱怨:

$I->sendPOST($this->url, ['email' => '']);
$I->seeResponseContainsJson(['error' => 'El correo electrónico es requerido.']);

1 认为(再次,我不确定),他的发生是因为class ABC { public: ABC() = default; // <-- ABC(ABC&&) = default; // <-- private: std::vector<std::unique_ptr<int>> up; std::function<void (int*)> func; }; 的默认副本被定义为{{ 1}}并且优先于默认的move-ctor(它仍然是格式错误,因为我们在这里处理不可复制的成员)。尝试生成默认的ABC移动结果:

noexcept

因此,强制生成no - noexcept move-ctor可以选择它。为什么Clang没有问题 - 我不知道。

上述一个解释是,删除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移动器。 std::function没有noexcept move-ctor(sucks tremendously),所以整个班级都会回复给复制者。由于std::function没有,所以整个事情都会破裂。

答案 1 :(得分:3)

这是因为std::function移动ctor不是noexcept,而std::vector只能使用移动ctor noexcept(强异常保证)。< / p>

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

如果您删除了std::function,则会发生以下情况:.resize()无需复制A.upstd::vector的移动运算符为{{1} }),所以noexcept s(在std::unique_ptr内)可以移动,一切正常。

请参阅this coliru:如果您对up发表评论,noexcept将需要复制所有内容,然后问题就会出现。

添加vec.reserve()修复了这个问题,因为用户声明(虽然ABC(ABC&&) = default ed)移动ctor会禁止生成复制构造函数(请参阅here)。

我们也可以采用相反的方式并在coliru中手动删除default的复制构造函数(并保持移动ctor A):{{3} }。

要解决此问题,您可以noexcept存储在std::function中,其中std::unique_ptr移动ctor。 here

答案 2 :(得分:1)

GCC接受了std :: function的移动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); }