P*******b 发帖数: 1001 | 1 f2()为啥比f1()多调用一次copy ctor
#include
struct C
{
C() { std::cout << "Ctor" << std::endl;}
C(const C&) { std::cout << "A copy was made.\n"; }
};
C f1(C c)
{
C first;
return first;
}
C f2(C c)
{
C first;
return c;
}
int main()
{
C first;
std::cout << "-----------" << std::endl;
C second = f1(first);
std::cout << "-----------" << std::endl;
f2(second);
} |
d****n 发帖数: 233 | 2 Why do you think f2 比f1()多调用一次copy ctor?
They are the same in term of # of calls to copy ctor.
【在 P*******b 的大作中提到】 : f2()为啥比f1()多调用一次copy ctor : #include : struct C : { : C() { std::cout << "Ctor" << std::endl;} : C(const C&) { std::cout << "A copy was made.\n"; } : }; : C f1(C c) : { : C first;
|
i**r 发帖数: 40 | 3 It might have to do with a compiler optimization technique call Return
Value Optimization(RVO).
Both f1 and f2 are pass-by-value and return-by-value, therefore
calling function f1 or f2 will make at least 2 copies of object.
The f1 function call:
C second = f1(first);
is within an assignment statement, in theory this statement should
call copy ctor 3 times.
However, since the return value of f1 is an rvalue, i.e. temporary,
and will be destroyed after the assignment operation, AND the return
val |
l*******g 发帖数: 4894 | 4 I am not sure about he RVO you mentioned.
for this question, ctor is called just every time "C first" executed where
the C() is called. Due to everytime it is a call by value and return by
value. so it has to make a copy for that. so the C(const C&) is called.
so the "A copy was made" are executed three time because two call by value
and one return by value.
【在 i**r 的大作中提到】 : It might have to do with a compiler optimization technique call Return : Value Optimization(RVO). : Both f1 and f2 are pass-by-value and return-by-value, therefore : calling function f1 or f2 will make at least 2 copies of object. : The f1 function call: : C second = f1(first); : is within an assignment statement, in theory this statement should : call copy ctor 3 times. : However, since the return value of f1 is an rvalue, i.e. temporary, : and will be destroyed after the assignment operation, AND the return
|
t****t 发帖数: 6806 | 5 why don't you try the code yourself on an NRVO enabled compiler, e.g. VC8 or
gcc. IamR is right.
【在 l*******g 的大作中提到】 : I am not sure about he RVO you mentioned. : for this question, ctor is called just every time "C first" executed where : the C() is called. Due to everytime it is a call by value and return by : value. so it has to make a copy for that. so the C(const C&) is called. : so the "A copy was made" are executed three time because two call by value : and one return by value.
|
l*******g 发帖数: 4894 | 6 I am using gcc to run the code. the result is following.
Ctor |
t****t 发帖数: 6806 | 7 oh i thought you were durbin. obviously you are not. :)
your explanation is correct, however your explanation can be applied to both
function, therefore it did not answer the original question: why one more c
opy ctor call?
while IamR's answer is right to the point.
【在 l*******g 的大作中提到】 : I am using gcc to run the code. the result is following. : Ctor
|
l*******g 发帖数: 4894 | 8 恩,其实我对于c++也不是很熟悉,所以说的不一定对。我的理解是:
对于第二个函数,因为返回的c是一个传递进来的copy值,所以在return的时候需要再
做一次copy,也就是lamR说的不是一个temp的值。
似乎我刚才第一遍说的也不对。不过确实帮助理解了RVO了。多谢!
both
c
【在 t****t 的大作中提到】 : oh i thought you were durbin. obviously you are not. :) : your explanation is correct, however your explanation can be applied to both : function, therefore it did not answer the original question: why one more c : opy ctor call? : while IamR's answer is right to the point.
|
t****t 发帖数: 6806 | 9 The returned value must be a local object for NRVO to take effect. That's th
e rule. The reason behind this is, the function does both "creating the loca
l object" and "creating the temporary" then NRVO can merge these two steps i
nto one. If the returned object is not locally created and compiler still wa
nt to do the optimization, then it has to do interprocedual optimization whi
ch makes the thing complex unnecessary. For example, in the second function,
the returned value is the parameter; t
【在 l*******g 的大作中提到】 : 恩,其实我对于c++也不是很熟悉,所以说的不一定对。我的理解是: : 对于第二个函数,因为返回的c是一个传递进来的copy值,所以在return的时候需要再 : 做一次copy,也就是lamR说的不是一个temp的值。 : 似乎我刚才第一遍说的也不对。不过确实帮助理解了RVO了。多谢! : : both : c
|
l*******g 发帖数: 4894 | 10 sounds more clear now. Thanks a lot!
th
loca
i
wa
whi
function,
fu
n
【在 t****t 的大作中提到】 : The returned value must be a local object for NRVO to take effect. That's th : e rule. The reason behind this is, the function does both "creating the loca : l object" and "creating the temporary" then NRVO can merge these two steps i : nto one. If the returned object is not locally created and compiler still wa : nt to do the optimization, then it has to do interprocedual optimization whi : ch makes the thing complex unnecessary. For example, in the second function, : the returned value is the parameter; t
|
j*****u 发帖数: 37 | 11 C++1x里面已经是语言的一部分了
另外感觉这玩意和尾递归有点像
The returned value must be a local object for NRVO to take effect. That's th
e rule. The reason behind this is, the function does both "creating the loca
l object" and "creating the temporary" then NRVO can merge these two steps i
nto one. If the returned object is not locally created and compiler still wa
nt to do the optimization, then it has to do interprocedual optimization whi
ch makes the thing complex unnecessary. For example, in the second function,
the
【在 t****t 的大作中提到】 : The returned value must be a local object for NRVO to take effect. That's th : e rule. The reason behind this is, the function does both "creating the loca : l object" and "creating the temporary" then NRVO can merge these two steps i : nto one. If the returned object is not locally created and compiler still wa : nt to do the optimization, then it has to do interprocedual optimization whi : ch makes the thing complex unnecessary. For example, in the second function, : the returned value is the parameter; t
|
z****e 发帖数: 2024 | 12 第一个函数的调用其实是类似这样的
C second = f1(first);
f1(&second, c),
这个second被直接构造了。
千万不要死扣C++编译器,把时间精力放在大宗面题上边更有效。这些犄角旮旯的东西
一般不是面试重点。 |
z****e 发帖数: 2024 | 13 master shifu又来这传道了?
th
loca
i
wa
whi
function,
fu
n
【在 t****t 的大作中提到】 : The returned value must be a local object for NRVO to take effect. That's th : e rule. The reason behind this is, the function does both "creating the loca : l object" and "creating the temporary" then NRVO can merge these two steps i : nto one. If the returned object is not locally created and compiler still wa : nt to do the optimization, then it has to do interprocedual optimization whi : ch makes the thing complex unnecessary. For example, in the second function, : the returned value is the parameter; t
|
t****t 发帖数: 6806 | 14 it has been a part of the language since c++98. and it is always optional.
th
loca
i
wa
whi
function,
fu
n
【在 j*****u 的大作中提到】 : C++1x里面已经是语言的一部分了 : 另外感觉这玩意和尾递归有点像 : : The returned value must be a local object for NRVO to take effect. That's th : e rule. The reason behind this is, the function does both "creating the loca : l object" and "creating the temporary" then NRVO can merge these two steps i : nto one. If the returned object is not locally created and compiler still wa : nt to do the optimization, then it has to do interprocedual optimization whi : ch makes the thing complex unnecessary. For example, in the second function, : the
|