【问题标题】:Why the below code does not compile with gcc but compiles fine with clang为什么下面的代码不能用 gcc 编译但用 clang 编译得很好
【发布时间】:2017-11-09 17:23:02
【问题描述】:

下面的代码可以用 clang 但不能用 gcc 编译,任何解释这是 gcc 中的错误吗?

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

【问题讨论】:

  • 请贴出编译器的错误信息。
  • 在 /opt/wandbox/gcc-7.1.0/include/c++/7.1.0/memory:80:0 包含的文件中,来自 prog.cc:2: /opt/wandbox/gcc -7.1.0/include/c++/7.1.0/bits/unique_ptr.h:388:7: 注意:这里声明的 unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~
  • 使用适当的格式将错误消息添加到您的帖子中。
  • 我认为联系一些编译器自己尝试并不难,因为错误消息非常难看。试试魔杖盒
  • 将错误信息添加到帖子中,谢谢

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


【解决方案1】:

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

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

如果你删除std::function,会发生这样的事情:.resize() 不需要复制A.upstd::vector 的移动运算符是noexcept),所以std::unique_ptrs ( up) 内部可以移动,一切正常。

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

添加ABC(ABC&amp;&amp;) = default 解决了这个问题,因为用户声明(尽管defaulted)移动ctor 禁止生成复制构造函数(请参阅here)。

我们也可以反其道而行之,手动删除coliru中A的拷贝构造函数(并保留move ctornoexcept):here


要解决此问题,您可以std::function 存储在具有nothrow 移动ctor 的std::unique_ptr 中。 See here

【讨论】:

  • 我的意思是noexcept move-ctor 的手动声明是关键。您根本不必触摸 copy-ctor,如果您尝试删除它,它将无法正常工作。
  • 不,那只是因为声明复制 ctor 会阻止声明移动 ctor。这就是为什么我链接到的 coliru 声明了 move ctor。
  • 根本原因是vector&lt;unique_ptr&gt;&gt; 假装它是可复制的,而实际上它不是。反过来,这会导致ABC 在不可复制时假装可复制。然后,vector&lt;ABC&gt; 说“你不能移动吗?不可以吗?你可以复制吗?可以吗?然后我会复制你,这样我们就有很强的异常安全性。”然后砰,爆炸。
【解决方案2】:

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

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

1认为(再次,我不确定),这是因为ABC 的默认copy-ctor 定义为noexcept 和因此比默认的 move-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;
    ^~~

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

上面的一个解释是删除std::function 允许生成默认的noexcept move-ctor。 std::function 没有 noexcept move-ctor(sucks tremendously),所以整个班级都退回到 copy-ctor。由于unique_ptr 没有,所以整个事情都崩溃了。

【讨论】:

  • 让我知道我是否应该去挖掘标准报价。
  • 只有vectorfunction 之一可以正确生成默认的move-ctor。奇怪...
  • 确实,只是这两者的结合导致了问题,而它们中的任何一个都可以正常编译。
【解决方案3】:

GCC 接受了 std::function 的 move ctor 上缺少 noexcept 作为错误

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); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2017-01-11
    • 2013-02-20
    • 2015-02-08
    • 2015-08-21
    相关资源
    最近更新 更多