为什么以下代码不使用GCC编译,而是用Clang编译罚款
Why the below code does not compile with gcc but compiles fine with clang
下面的代码在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); }
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- clang 插件:在编译过程中修改 AST
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 通过自制软件(macOS)安装的clang++:编译错误
- 在使用 Clang 编译 DLL 时指定 DEF 文件
- 获取 clang++:错误:在编译文件系统库的代码时
- 如何在 clang 的自动会议中检查支持编译标志
- constexpr 使用 clang 编译 TensorFlow 时出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 如何更改 clang 的预编译头文件的路径
- 除了说明符神秘地破坏编译(Clang,GCC不同意)
- VS 代码显示编译 clang 的错误消息
- 同一段代码,可以用 xcode 或使用 g++ 编译,在终端中编译 clang++,不能使用 gcc 或 clang 编
- 简单的 constexpr 函数无法使用 GCC 编译(clang 没问题)
- 在 C++14 模式下为 libstdc++ 编译 clang 正则表达式程序会导致错误
- 如何在Windows上编译Clang
- 在Windows中编译Clang插件
- 是否可以在内存中使用Clang c++ API编译Clang源代码?