【发布时间】:2020-11-20 15:29:32
【问题描述】:
我很想知道函数的参数何时被复制。
#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
}
函数是否从一开始就复制它的参数?如果不是,我们如何知道参数何时被复制?从上面的代码中,我假设参数没有被复制,因为 std::move 仍然影响变量 x。
【问题讨论】:
-
//print nothing-- 不,这是不正确的。我不确定你是如何运行它的,但是when I run it,xis not changed.