 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tomás Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Rusty Surprises! |
|
|
I recently started back programming in C++, having taken a break of well
over a year. Before I stopped, I considered myself to be an expert.
When I started back, things started coming back to me day by day. When I
first opened a source code file it looked like gibrish!
Now about two weeks later, I just about back to the standard I was at.
But just there today I gave myself a little suprise as I read over some
of my old code. I had something akin to the following:
std::string GetString();
int main()
{
std::string const &str = GetString();
}
My very first thought was, "What the hell is that doing? The code is
creating a reference to a temporary!". Over the next five seconds, my
brain must have by delving into crevases deep at the back of my mind, as
I remembered this nice little feature whereby it's perfectly legal to
bind a const reference to a function's return value.
If I hadn't read over that code today, then I'd still be doing the likes
of:
std::string GetString();
int main()
{
std::string const str = GetString();
// or
std:: string const str( GetString() );
}
whereby I the compiler is more than entitled to invoke a copy construct.
Just thought I'd share that little suprise!
Does many of you here bind cost references to return values?
-Tomás |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Rusty Surprises! |
|
|
Tomás wrote:
| Quote: | [..]
Does many of you here bind cost references to return values?
|
All the time.
Consider:
// .. necessary includes, usings, etc.
void print_a_string(const std::string& somestring) {
cout << somestring << endl;
}
std::string what_to_print() {
return "42";
}
int main() {
print_a_string(what_to_print()); // here.
}
V
--
Please remove capital As from my address when replying by mail |
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Rusty Surprises! |
|
|
| Quote: | return "42";
...
???
Need I ask where that comes from?
Drew
|
It's the "answer to the great question of life, the universe, and
everything", according to Douglas Adams's books /The Hitchhikers Guide to
the Galaxy/.
Programmers frequently use that number for meaningless examples.
| Quote: | Is this another one of your optimisation techniques?
One of many ; ) .
-Tomás
|
Premature optimization is bad for your code. Always write simple clear code
that humans can understand.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!! |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Typo Alert! |
|
|
Tomás wrote:
| Quote: | My brain's a bit fuzzy today, I'll repost that without the typos:
[...]
|
Oh, come on!... |
|
| Back to top |
|
 |
Ben Pope Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Rusty Surprises! |
|
|
Tomás wrote:
| Quote: | I recently started back programming in C++, having taken a break of well
over a year. Before I stopped, I considered myself to be an expert.
When I started back, things started coming back to me day by day. When I
first opened a source code file it looked like gibrish!
Now about two weeks later, I just about back to the standard I was at.
But just there today I gave myself a little suprise as I read over some
of my old code. I had something akin to the following:
std::string GetString();
int main()
{
std::string const &str = GetString();
}
My very first thought was, "What the hell is that doing? The code is
creating a reference to a temporary!". Over the next five seconds, my
brain must have by delving into crevases deep at the back of my mind, as
I remembered this nice little feature whereby it's perfectly legal to
bind a const reference to a function's return value.
If I hadn't read over that code today, then I'd still be doing the likes
of:
std::string GetString();
int main()
{
std::string const str = GetString();
// or
std:: string const str( GetString() );
}
whereby I the compiler is more than entitled to invoke a copy construct.
|
Not only is it entitled to, but it also requires an accessible copy
constructor.
But so what? It's also entitled to elide that copy constructor in many
cases (RVO, NRVO).
Is this another one of your optimisation techniques?
Ben Pope
--
I'm not just a number. To many, I'm known as a string... |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Typo Alert! |
|
|
My brain's a bit fuzzy today, I'll repost that without the typos:
I recently started back programming in C++, having taken a break of well
over a year. Before I stopped, I considered myself to be an expert.
When I started back, things started coming back to me day by day. When I
first opened a source code file it looked like gibrish!
Now about two weeks later, I'm just about back to the standard I was at.
But just there today I gave myself a little suprise as I read over some
of my old code. I had something akin to the following:
std::string GetString();
int main()
{
std::string const &str = GetString();
}
My very first thought was, "What the hell is that doing? The code is
creating a reference to a temporary!". Over the next five seconds, my
brain must have by delving into crevases deep at the back of my mind, as
I remembered this nice little feature whereby it's perfectly legal to
bind a const reference to a function's return value.
If I hadn't read over that code today, then I'd still be doing the likes
of:
std::string GetString();
int main()
{
std::string const str = GetString();
// or
std:: string const str( GetString() );
}
whereby the compiler is more than entitled to invoke a copy construct.
Just thought I'd share that little suprise!
Do many of you here bind cost references to return values?
-Tomás |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Rusty Surprises! |
|
|
| Quote: | Is this another one of your optimisation techniques?
|
One of many ; ) .
-Tomás |
|
| Back to top |
|
 |
d.f.s. Guest
|
Posted: Mon Feb 27, 2006 6:06 pm Post subject: Re: Rusty Surprises! |
|
|
[snip]
???
Need I ask where that comes from?
Drew |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Mon Feb 27, 2006 7:06 pm Post subject: Re: Rusty Surprises! |
|
|
* Tomás:
| Quote: | int main()
{
std::string const &str = GetString();
}
[snip] |
| Quote: | If I hadn't read over that code today, then I'd still be doing the likes
of:
std::string GetString();
int main()
{
std::string const str = GetString();
// or
std:: string const str( GetString() );
}
whereby I the compiler is more than entitled to invoke a copy construct.
|
The copy constructor can be called in all three cases, is required to be
accessible in all three cases, and the call can be elided in all three
cases.
The first is simply bad form because, as you found out, it can be confusing.
Bjarne decided to allow that form just to make the rules for reference
binding conceptually "as simple as possible".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|