将RVO对象传递到std::vector::template_back
Pass RVO object to std::vector::emplace_back
考虑以下数组:
std::vector<Foo> arr;
arr.emplace_back(calculate_foo());
emplace_back
受益于消除临时对象,因为它的参数在新构造的元素上传递到位(因此这里的template_back将触发移动或复制构造函数(Foo(foo)
当然,上面的代码在calculate_foo
中创建了一个临时对象,然后根据Foo的构造函数将其复制或移动到新数组中。
有没有机会加快速度并消除临时对象?
您无法避免使用emplace_back
为Foo
的构造函数创建临时参数。如果使用类型为Foo
的参数,则该参数是将传递给模板对象的构造函数的临时对象。在这种情况下,push_back
将同样有效。
要真正利用定位,您的类型需要有一个构造函数,它接受一些轻量级参数,可以用来构造更昂贵的对象。这样,只有临时对象才是轻量级对象。为了直接从返回值中进行模板设置,该构造函数只能接受一个参数。
示例:
struct ExpensiveMove {
explicit ExpensiveMove(double d) {
std::cout << "constructn";
std::fill(arr.begin(), arr.end(), d);
}
ExpensiveMove(const ExpensiveMove&) { std::cout << "expensive copyn"; }
ExpensiveMove(ExpensiveMove&&) { std::cout << "expensive moven"; }
ExpensiveMove& operator=(const ExpensiveMove&) { std::cout << "expensive copy assn"; return *this; }
ExpensiveMove& operator=(ExpensiveMove&&) { std::cout << "expensive move assn"; return *this; }
std::array<double, 1024> arr;
};
double calculate()
{
return 4.2;
}
int main() {
std::vector<ExpensiveMove> arr;
arr.emplace_back(calculate());
}
本例中没有ExpensiveMove
类型的临时存储器。有一个临时替身,这并不重要。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 为什么(-1)%vector::size()总是返回0
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 传递给std::function template的template参数究竟代表什么
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 在template中使用std::variant的template函数
- 为什么std::vector比数组慢
- 为什么std::vector::template在没有调用任何复制构造函数的情况下调用析构函数