由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - one C++ question
相关主题
请教C/C++小问个简单coding问题
请教一个c的概念题[请教] C++ coding question
C++ Q42: (C22)Mathworks is hiring! job #10319 - C++ Developer – Compile
问一个低级的问题问个C/C++概念的问题
C++ Q83: 这个const_cast什么意思?【为什么我写的reverse string总出错】
请问关于overloading << (转载)问一个C++问题:default parameter and overriding/inheritanc
问个C++ ctor的问题问一道无聊的bloomberg电面题
Why I can't compile this function successfullyJava system architect/lead developer opening in a cloud computing company
相关话题的讨论汇总
话题: std话题: f1话题: value话题: first话题: cout
进入JobHunting版参与讨论
1 (共1页)
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

1 (共1页)
进入JobHunting版参与讨论
相关主题
Java system architect/lead developer opening in a cloud computing companyC++ Q83: 这个const_cast什么意思?
最新微软SDE II面试题请问关于overloading << (转载)
讨论一个OO问题问个C++ ctor的问题
请问这段程序有什么bug?Why I can't compile this function successfully
请教C/C++小问个简单coding问题
请教一个c的概念题[请教] C++ coding question
C++ Q42: (C22)Mathworks is hiring! job #10319 - C++ Developer – Compile
问一个低级的问题问个C/C++概念的问题
相关话题的讨论汇总
话题: std话题: f1话题: value话题: first话题: cout