g*********s 发帖数: 1782 | 1 vector& f(int x) {
vector res;
for ( int i = 0; i < x; ++ i )
res.push_back(i);
return res;
}
void f(int x, vector& res) {
for ( int i = 0; i < x; ++ i )
res.push_back(i);
} |
X****r 发帖数: 3557 | 2 The former is wrong.
【在 g*********s 的大作中提到】 : vector& f(int x) { : vector res; : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : return res; : } : void f(int x, vector& res) { : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : }
|
e****d 发帖数: 895 | 3 Do you mean to return vector instead of vector&
in the first case? Otherwise, you should const-qualify
it.
【在 g*********s 的大作中提到】 : vector& f(int x) { : vector res; : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : return res; : } : void f(int x, vector& res) { : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : }
|
t****t 发帖数: 6806 | 4 returning a const& to local object is wrong too...
【在 e****d 的大作中提到】 : Do you mean to return vector instead of vector& : in the first case? Otherwise, you should const-qualify : it.
|
g*********s 发帖数: 1782 | 5 what if i need to return the reference for convenience?
【在 t****t 的大作中提到】 : returning a const& to local object is wrong too...
|
y***d 发帖数: 2330 | 6 then it is a trouble for the future.
【在 g*********s 的大作中提到】 : what if i need to return the reference for convenience?
|
S**I 发帖数: 15689 | 7 you can't do this, it's just wrong.
【在 g*********s 的大作中提到】 : what if i need to return the reference for convenience?
|
e****d 发帖数: 895 | 8 That's right. Should not return reference to local object.
But are the following two cases valid?
extern const vector & foo(); //return reference to local obj.
case 1,
vector v(foo());
case 2,
foo().size();
【在 t****t 的大作中提到】 : returning a const& to local object is wrong too...
|
z****e 发帖数: 2024 | 9 师傅给展开讲讲吧。这个我感觉很多地方都用。
【在 t****t 的大作中提到】 : returning a const& to local object is wrong too...
|
S**I 发帖数: 15689 | 10 could be valid if the vector is not a local object created within foo().
【在 e****d 的大作中提到】 : That's right. Should not return reference to local object. : But are the following two cases valid? : extern const vector & foo(); //return reference to local obj. : case 1, : vector v(foo()); : case 2, : foo().size();
|
|
|
t****t 发帖数: 6806 | 11 哪里用?
【在 z****e 的大作中提到】 : 师傅给展开讲讲吧。这个我感觉很多地方都用。
|
t****t 发帖数: 6806 | 12 no, they are all undefined.
【在 e****d 的大作中提到】 : That's right. Should not return reference to local object. : But are the following two cases valid? : extern const vector & foo(); //return reference to local obj. : case 1, : vector v(foo()); : case 2, : foo().size();
|
z****e 发帖数: 2024 | 13 比如op[]的一个overload的const版本,就有人返回const&
member function 返回const&的是不是不算是你说得case?
【在 t****t 的大作中提到】 : 哪里用?
|
p***o 发帖数: 1252 | 14 That's not a reference to a LOCAL variable.
【在 z****e 的大作中提到】 : 比如op[]的一个overload的const版本,就有人返回const& : member function 返回const&的是不是不算是你说得case?
|
z****e 发帖数: 2024 | 15 是的,但可以在stack上面。原object销毁的时候咋办。
【在 p***o 的大作中提到】 : That's not a reference to a LOCAL variable.
|
t****t 发帖数: 6806 | 16 almost any object can be on stack.
【在 z****e 的大作中提到】 : 是的,但可以在stack上面。原object销毁的时候咋办。
|
e****d 发帖数: 895 | 17 You should not access any non-static data member of an
object that has been destroyed.
【在 z****e 的大作中提到】 : 是的,但可以在stack上面。原object销毁的时候咋办。
|
p***o 发帖数: 1252 | 18 I don't get your point. Show your code.
【在 z****e 的大作中提到】 : 是的,但可以在stack上面。原object销毁的时候咋办。
|
d****p 发帖数: 685 | 19 This case is different.
The object associated with the returned reference is on a stack with longer
lifecycle or from heap.
【在 z****e 的大作中提到】 : 比如op[]的一个overload的const版本,就有人返回const& : member function 返回const&的是不是不算是你说得case?
|
z****e 发帖数: 2024 | 20 大师也多日不见了。
偶新年得了个offer,可惜公司后来说高层人事调整,offer又被收回。
我真是服了。
longer
【在 d****p 的大作中提到】 : This case is different. : The object associated with the returned reference is on a stack with longer : lifecycle or from heap.
|
|
|
h***i 发帖数: 1970 | 21 return vector就行,编译器自动优化。
【在 g*********s 的大作中提到】 : vector& f(int x) { : vector res; : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : return res; : } : void f(int x, vector& res) { : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : }
|
e****d 发帖数: 895 | 22 RVO will apply when the returned value is used
to construct another vector.
Or wait until the move semantics has been implemented in stl.
【在 h***i 的大作中提到】 : return vector就行,编译器自动优化。
|
t****t 发帖数: 6806 | 23 move semantics has been implemented in stl for gcc.
【在 e****d 的大作中提到】 : RVO will apply when the returned value is used : to construct another vector. : Or wait until the move semantics has been implemented in stl.
|
g*********s 发帖数: 1782 | 24 sorry, u guys mean the following won't trigger an extra vector copy?
vector f(int x) {
vector res;
for ( int i = 0; i < x; ++ i )
res.push_back(i);
return res;
}
【在 t****t 的大作中提到】 : move semantics has been implemented in stl for gcc.
|
e****d 发帖数: 895 | 25 vector<> of c++0x stl will contain move constructor / move
assignment operator.
【在 g*********s 的大作中提到】 : sorry, u guys mean the following won't trigger an extra vector copy? : vector f(int x) { : vector res; : for ( int i = 0; i < x; ++ i ) : res.push_back(i); : return res; : }
|