There is an article on move constructors Move Constructors by Andrei
Alexandrescu(http://www.cuj.com/documents/s=8246/cujcexp2102alexandr/#2).
He has given two instances where temporaries are getting created.
Namely,
vector<string> ReadFile();
vector<string> vec = ReadFile();
and
string s1, s2, s3;
....
s1 = s2 + s3;
But can't we avoid the temporaries if we define
vector<string>& ReadFile();
and
string &operator+(string &rhs);
i.e. return references.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
shiju.cplusplus (6)
|
3/20/2005 11:34:49 PM |
|
falcon wrote:
> There is an article on move constructors Move Constructors by Andrei
> Alexandrescu(http://www.cuj.com/documents/s=8246/cujcexp2102alexandr/#2).
>
>
> He has given two instances where temporaries are getting created.
> Namely,
>
> vector<string> ReadFile();
> vector<string> vec = ReadFile();
>
> and
>
> string s1, s2, s3;
> ...
> s1 = s2 + s3;
>
>
> But can't we avoid the temporaries if we define
>
> vector<string>& ReadFile();
>
> and
>
> string &operator+(string &rhs);
>
> i.e. return references.
Effective C++ by Scott Meyers contains an extensive discussion on why
you shouldn't return references when you ought to return values.
Andrei
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Andrei
|
3/21/2005 7:00:00 AM
|
|
"falcon" <shiju.cplusplus@gmail.com> wrote in message
news:1111328456.324834.152770@f14g2000cwb.googlegroups.com...
> vector<string> ReadFile();
....
>
> But can't we avoid the temporaries if we define
>
> vector<string>& ReadFile();
....
> i.e. return references.
The context addressed, I believe, would involve returning a reference to a
local, which would no longer exist upon return. The problem and its
solutions are worth learning well. In general, pay special attention to the
lifetimes of objects. The rules are trivial and mundane once you have them
in hand, but hideously troublesome and a major cause of sleeplessness if you
don't. Andrei's article seems as good a starting point as any.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Boat
|
3/21/2005 4:19:11 PM
|
|