C++ 什么时候复制函数参数?

C++ 什么时候复制函数参数?,c++,c++11,stdmove,C++,C++11,Stdmove,我想知道函数的参数何时被复制 #include <vector> void foo (std::vector<float> a) { std::vector<float> y = std::move(a); //do something with vector y } int main() { std::vector<float> x {1.0f, 2.0f, 3.0f}; std::cout << x.at

我想知道函数的参数何时被复制

#include <vector>

void foo (std::vector<float> a)
{
   std::vector<float> y = std::move(a);
   //do something with vector y
}

int main()
{
   std::vector<float> x {1.0f, 2.0f, 3.0f};
   std::cout << x.at(0) << " " << x.at(1) << " " << x.at(2) << std::endl; //1.0 2.0 3.0
   foo(x);
   std::cout << x.at(0) << " " << x.at(1) << " " << x.at(2) << std::endl; //print nothing
}

#包括
void foo(标准::向量a)
{
std::vector y=std::move(a);
//用向量y做点什么
}
int main()
{
std::向量x{1.0f,2.0f,3.0f};
标准::cout
函数是否从一开始就复制其参数

在输入
foo()
主体之前,
a
参数在
main()
内的调用站点复制

从上面的代码中,我假设参数不会被复制,因为std::move仍然影响变量
x

a
参数是按值传递的,因此
a
复制了
x
。然后
foo()
的副本移动到
y
x
不受影响,这与您的声明相反


如果
a
参数是通过引用传递的,或者您
d
x
移动到
a
,则
x
将受到影响。

//不打印任何内容
--不,这是不正确的。我不确定您是如何运行此操作的,但是。