c – 执行stl复制功能?

以下是std :: copy函数的一些实现细节(来自vs2015):

template<class _InIt,
    class _OutIt> inline
    _OutIt _Copy_impl(_InIt _First, _InIt _Last,
        _OutIt _Dest, _Scalar_ptr_iterator_tag)
    {   // copy [_First, _Last) to [_Dest, ...), pointers to scalars
    ptrdiff_t _Count = _Last - _First;
    _CSTD memmove(&*_Dest, &*_First,
        _Count * sizeof (*_First));
    return (_Dest + _Count);
    }

似乎我们可以在标量类型的情况下使用memmove.但是为什么我们不能使用memmove如果它是POD类型(C 11)?据我所知,它既微不足道又
标准布局.

最佳答案 特殊要求是“可轻易复制的”.

For any trivially copyable type T, if two pointers to T point to
distinct T objects obj1 and obj2, where neither obj1 nor
obj2 is a base-class subobject, if the underlying bytes (1.7) making
up obj1 are copied into obj2, 43 obj2 shall subsequently hold
the same value as obj1.

43) By using, for example, the library functions (17.6.1.2)
std::memcpy or std::memmove.

其中包括POD和标量符号.但VC的要求比必要的要窄. libstdc’也是如此,他要求类型完全无关紧要.仅针对is_trivially_copy_assignable的libc测试.

点赞