在gcc 5.3中,以下示例中的两个函数都会生成对的调用memmove
。生成amemcpy
是否不合适?
#include <vector>
int blackhole(const std::vector<int>&);
int copy_vec1(const std::vector<int>& v1) {
const std::vector<int> v2{v1.begin(), v1.end()};
return blackhole(v2);
}
int copy_vec2(const std::vector<int>& v1) {
const auto v2 = v1;
return blackhole(v2);
}
我尝试使用g ++ 6.1.0编译此代码。我不确定所有细节,但我认为该memmove
调用不是由编译器直接生成的;它不是由编译器直接生成的。而是在实现的代码中<vector>
。
当我使用预处理代码时
/o/apps/gcc-6.1.0/bin/g++ -E -std=c++14 c.cpp
我看到有两个电话打来__builtin_memmove
,都来自.../include/c++/6.1.0/bits/stl_algobase.h
。查看该头文件,我看到以下注释:
// All of these auxiliary structs serve two purposes. (1) Replace
// calls to copy with memmove whenever possible. (Memmove, not memcpy,
// because the input and output ranges are permitted to overlap.)
// (2) If we're using random access iterators, then write the loop as
// a for loop with an explicit count.
我认为正在发生的事情是,用于复制向量的代码通常更适用于可以重叠的副本(例如对std::move
(?)的调用)。
(我尚未确认memmove
出现在程序集列表中的__builtin_memmove
调用与中的调用相对应stl_algobase.h
。我邀请其他人对此进行跟进。)
根据实现的不同,相对于memmove()
可能会有一些开销memcpy()
,但是差异很小。为不能重叠的副本创建特殊情况的代码可能不值得。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句