avatar
Google Map + dash cam# Apple - 家有苹果
x*s
1
比如小秘叫Rachel Green
大家写email就直接 “Dear Rachel,”吗?感觉没那么亲近。
avatar
P*b
2
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);
}
avatar
c*n
3
关于这部《战狼2》那是真的火了,火的不仅仅是这部电影,还捧红了吴京这个演了半
辈子戏的男演员以及过气的张翰还有那个不太出名的混血儿女演员和“达康书记”,说
实话能捧火这些比较有实力的演员我还是很欣慰的,最起码他们所出演的戏和那些用小
鲜肉的戏不一样,一个是看演技,另外一个是看颜值。但是在这部电影中有一个人估计
很多人都忘了,那就是一直都没有怎么露面的余男。
余男很多人都不认识,我就简单的说一下关于她的资料吧,很多的好莱坞大片中所出现
的中国女演员的影子其中有三四成都是这个性感的余男,虽然说她一直在好莱坞混但是
也没有红起来,这并不能说明余男演的不好,相反,余男的演技真的很牛逼。准确的来
说余男出道了十八年,拿下了十一个影帝的称号,而且都还是国际级别的。柏林电影节
比较有分量吧?很多中国演员也都希望能够拿到一个柏林电影节的奖项,而余男曾经担
任过柏林电影节的评委,别觉得这没什么,因为在柏林电影节的历史上中国只有两个中
国人担任过评委,一个是余男,另外一个是巩俐。
avatar
c*h
4
Google Map出来了,希望再加一个dash cam功能。
avatar
T*g
5
人家发信机器人都敢直接用first name,你一个大活人却不敢……
avatar
d*n
6
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;

avatar
b*d
7
直接用dear Rachel,没有问题.如果要想更正式,就用Ms. Green.
avatar
i*r
8
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
avatar
a*u
9
已经入职了就Dear Rachel好了。还在面试就Dear Ms. Green.
avatar
l*g
10
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

avatar
n*g
11
Rachel
avatar
t*t
12
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.

avatar
l*g
13
I am using gcc to run the code. the result is following.
Ctor
avatar
t*t
14
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

avatar
l*g
15
恩,其实我对于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.

avatar
t*t
16
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

avatar
l*g
17
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

avatar
j*u
18
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

avatar
z*e
19
第一个函数的调用其实是类似这样的
C second = f1(first);
f1(&second, c),
这个second被直接构造了。
千万不要死扣C++编译器,把时间精力放在大宗面题上边更有效。这些犄角旮旯的东西
一般不是面试重点。
avatar
z*e
20
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

avatar
t*t
21
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。