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 8 1533
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
Tomás wrote: [..] 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
Tomás wrote: My brain's a bit fuzzy today, I'll repost that without the typos: [...]
Oh, come on!...
Tomás wrote: 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... Is this another one of your optimisation techniques?
One of many ; ) .
-Tomás
[snip] return "42";
....
???
Need I ask where that comes from?
Drew
>> 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. 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!!!
* Tomás: int main() { std::string const &str = GetString(); }
[snip]
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Martin_Hurst |
last post by:
It would appear that MySQL is making great strides into the commercial and
even the enterprise arena.
I am not seeing the same news coverage being said about Postgresql.
I believe Postgresql has been a much better rdbms than MySQL for a long
time, so then why has not Postgresql grabbed the commercial and enterprise
arena before MySQL did?
...
|
by: David Hearn |
last post by:
I've been doing ASP.NET for the past few years and have gotten spoiled and
now find myself in a situation where I need to make some changes to an old
classic ASP page. The problem is that there is a VB script function that
populates an array. There are some dropdown boxes on the page that cause the
page to reload. When it does reload, it...
|
by: Jack B |
last post by:
I'm using Access 2002 to create a database for a small opera company that my
wife is involved in, and I'm more than a bit rusty because I haven't created
a new Access database since about 1999. So, I will probably have quite a
few questions as I go through this, and hopefully this group will help me
out.
I will be creating this database...
|
by: C C++ C++ |
last post by:
what is an escaping variable?
|
by: Raz |
last post by:
On Wed, 30 Apr 2008 21:54:37 GMT, Erik Wikströ
As I said, I will find the link again. His knowledge of C++ is just
fine.
http://www.builderau.com.au/video/soa/Rusty-s-message-to-C-programmers/0,2000064338,22432924p,00.htm
http://www.builderau.com.au/video/soa/Why-C-remains-relevant/0,2000064338,22432921p,00.htm
| |
by: Sam |
last post by:
Hi there. I hope this code is standard C. I think it is. Anyway, I
haven't used C in a Looooooong time and am a little rusty on pointers.
I am wondering why "buff" is a pointer to a pointer instead of just
"*buff". I'm also not really sure about the language of pointers. Is
it correct to say "pointer to a pointer to buff" or "double...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |