作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 libstdc++ STL_uninitialized.h 中看到这段代码:
// This class may be specialized for specific types.
// Also known as is_trivially_relocatable.
template<typename _Tp, typename = void>
struct __is_bitwise_relocatable
: is_trivial<_Tp> { };
template <typename _Tp, typename _Up>
inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
__relocate_a_1(_Tp* __first, _Tp* __last,
_Tp* __result, allocator<_Up>&) noexcept
{
ptrdiff_t __count = __last - __first;
if (__count > 0)
__builtin_memmove(__result, __first, __count * sizeof(_Tp));
return __result + __count;
}
但在我看来,memmove 对于可简单复制的对象来说是很好的,即使它们不是一般默认可构造的。在这种情况下谁关心默认构造函数?
最佳答案
我在 bugzilla 线程上询问了这个问题,他们向我指出了 https://stackoverflow.com/questions/47464819 。本质上,memcpy 不足以启动对象的生命周期,除非默认构造函数也很简单。因此,从技术上来说,就内存中的位而言,它可能是没问题的,但根据标准,这将是未定义的行为。由于这个库代码是由编译器团队编写的,如果他们认为安全,他们就有权这样做,但他们显然不相信安全性。
关于c++ - 为什么 std::vector 需要 is_trivial 来进行按位移动,而不仅仅是 is_trivially_copyable ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690019/
在制作简单的pod-classes时,似乎我必须在安全和性能之间做出选择。发生这种情况是因为 std::is_trivial , 它在 STL 中用于确定是否可以通过 memmove 复制一个类/me
我在 libstdc++ STL_uninitialized.h 中看到这段代码: // This class may be specialized for specific types. /
我是一名优秀的程序员,十分优秀!