[英]Do I need to std::move an std::initializer_list?
I want to pass a list-initialized vector to a worker function through a wrapper.我想通过包装器将列表初始化的向量传递给工作人员 function。 I don't need the values in the original function (main), so should I move it?
我不需要原始 function(主)中的值,所以我应该移动它吗?
The doc states that:该文档指出:
copying a std::initializer_list does not copy the underlying objects.
复制 std::initializer_list 不会复制底层对象。
So costs are probably minimal, but is there still an advantage to moving it or is the list copy-elided and the vector directly list-initialized?所以成本可能是最小的,但是移动它仍然有优势,还是列表复制省略并且向量直接列表初始化?
(Compiling) Code : (编译)代码:
#include <iostream>
#include <vector>
void worker(std::vector<int>&& some_ints)
{
std::cout << "I hope my ints arrive here without further overhead :)" << std::endl;
}
void wrapper(std::vector<int>&& some_ints)
{
worker(std::move(some_ints));
}
int main()
{
wrapper( { 1, 5 } ); // <-- std::move here?
}
A braced-init-list is not a std:initializer_list
.支撑初始化列表不是
std:initializer_list
。
Here:这里:
wrapper( { 1, 5 } );
overload resolution finds the void wrapper(std::vector<int>&&)
overload which is a viable overload for the call: creating a temporary std::vector<int>
object (by list-initialization via the std::initializer_list<T>
constructor of std::vector<T>
) which binds to the rvalue reference parameter of the wrapper
function.重载解析找到
void wrapper(std::vector<int>&&)
重载,这是调用的可行重载:创建临时std::vector<int>
object (通过列表初始化通过std::initializer_list<T>
std::vector<T>
> 的构造函数)绑定到wrapper
function 的右值引用参数。
Trying to move a braced-init-list试图移动一个支撑初始化列表
wrapper(std::move({ 1, 5 })); // error
is illegal ( T
in the std::move<T>(T&&)
function template cannot be deduced).是非法的(不能推断出
std::move<T>(T&&)
T
模板中的 T )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.