473,699 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird return statement

Hello all,

I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.

TIA

string PublishedProduc tsRepo :: CreateStatement () const {
string statement;

statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statemen t); }
Mar 26 '06
13 1863
* Bob Hairgrove:
On Sun, 26 Mar 2006 18:27:14 GMT, "Tomás" <NU**@NULL.NULL > wrote:
3) A proficient programmer would probably write:

string PublishedProduc tsRepo :: CreateStatement () const
{
return
"SELECT DISTINCT "
"* "
"FROM "
"map";
}


That should be:

std::string PublishedProduc tsRepo :: CreateStatement () const
{
return std::string(
"SELECT DISTINCT "
"* "
"FROM "
"map");
}


No, you don't need the explicit constructor call, because that
constructor is not 'explicit'.

For e.g. a std::auto_ptr you would need the explicit construction.

For std::string you don't.

--
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?
Mar 26 '06 #11
On Sun, 26 Mar 2006 22:44:05 +0200, "Alf P. Steinbach"
<al***@start.no > wrote:
* Roland Pibinger:
In general, it's not recommendable to make your code dependent on
language hacks like RVO.


But not this.

On the contrary, it's absolutely not a good idea to resort to premature
optimization (Google for "premature optimization"): it wastes programmer
time and may end up making your program less efficient rather than more.


In this case RVO is the immature optimization :-)
I don't recommend any optimization at all.

Best regards.
Roland Pibinger
Mar 26 '06 #12
Bob Hairgrove wrote:
That should be:

std::string PublishedProduc tsRepo :: CreateStatement () const
{
return std::string(
"SELECT DISTINCT "
"* "
"FROM "
"map");
}


Why?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 26 '06 #13
* Phlip:
Tomás wrote:
1) The "statement" object is default constructed, and the an assignment
is performed. This is inefficent -- it should have just been a
construction:

string statement("bla bla");
The compiler is required to directly call the constructor, even if = is
used.


Tomás was referring to the assignment operator call, I think.

2) There's no point in creating that nameless temporary in the "return"
statement. It could simply be:

return statement;


I do know that Return Value Optimization could make one of the strings
inside the function become an alias for the final string outside the
function. But I don't know how aggressive that rule is.


Not very, it applies to temporaries and returning a named local
variable. But that's enough here, and what kicks in here anyway is the
"as if" rule. std::string is a class known by the compiler, and it can
optimize as it wants to, regardless of the RVO rule.

Suppose the copy
constructor for std::string had a side effect that you could count. RVO will
make one of those side effects go away. (That's why it's a permitted
optimization and defined as "lossy"; so programmers will know better than to
depend on the number of side-effects from such constructors.)

Will RVO make all of this go away?

return string(string(s tring(string(st ring(string(sta tement))))));


RVO /can/ make all of that go away, "whenever a temporary class object
is copied using a copy constructor".

Whether it will or not depends on the compiler etc.
--
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?
Mar 26 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1453
by: HelLind | last post by:
Hello people, I am having a weird error and don't know what term to search in google. Sorry to bother. There is data in my field "M_BIO" When I run this code <%=rslist.Fields.Item("M_bio").Value%>, it displays the data, no problem.
2
6770
by: alice | last post by:
Hi, When I compiles the following program with g++, it gives the following output: # g++ -o list list.C list.C: In function `int main()': list.C:116: jump to case label list.C:110: crosses initialization of `std::string data' list.C:120: jump to case label list.C:110: crosses initialization of `std::string data'
5
1918
by: crunix | last post by:
Hello. I have developed a medical application with php 4 linked to IBM DB2 database. The database connection is right and i can access data with no problem...but sometimes when i reload the page which has been already loaded (by pressing CTRL-R) i receive an SQL STATE Error: "(...) SQL State: X (..)"
8
1920
by: G Patel | last post by:
Can people please comment on the layout/style of my problem? The major issue I had was the layout. I ended up having to put a relatively large switch statement, inside an if statement, which is inside a while loop. If someone can tell me how I can rearrange these elements a little to make it cleaner, I would appreciate that. I've tested it on the command line, and it works *well*, but the source code layout is bugging me. I'd also...
3
1056
by: Val | last post by:
In vc7 (studio 2002), when I try to debug the first "if" statement, the IDE jumps to the next valid line and evaluates it even if the if-statement is false. What is going on? if( (theDealer.m_nPoints == 15) && (theDealer.Hand.size() == 2) ) //Break Point set here but... { vector<Card>::iterator vdi; // ... runs to here once debugging starts. Even if false is returned initially. vdi = find_if( theDealer.Hand.begin(), theDealer.Hand.end(),...
4
1385
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private Function AddSystem(ByVal txt As TextBox, ByVal cn As OracleConnection) As Integer Try
5
2411
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return true;
5
1710
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)" % self return {"a": "b", "c": "d"} and then I have another module called SensorSingleton that emulates the
11
1381
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the old one. i thought what happend inside a function stays inside a function meaning what comes out is independent of what comes in. Meaning if I want the list I send as a parameter to the function I have to do x = func(x) and not just func(x) and x...
0
9032
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
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
3
2008
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.