473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

return values and type conversions

I am wordering if there is any difference between these two statements:

std::string functionX()
{
char buf[256];
... do stuff;
return string(buf);
}

std::string functionX()
{
char buf[256];
... do stuff;
return buf;
}

Both are acceptable to my compiler so obviously it is smart enough to
know I want buf translated to a string. However, my question relates to
what happens in this case:

std::string x = functionX();

In both cases the char* has to be converted to a string, but is the copy
constructor also called in both cases or is it smart enough to call the
conversion and assign it directly to string 'x'? Are there any unseen
problems with either definition of functionX?

NR

Jul 19 '05 #1
3 2817

"Noah Roberts" <nr******@dontemailme.com> wrote in message news:vo************@corp.supernews.com...
I am wordering if there is any difference between these two statements:
In both cases buf is converted to string type (in one case implicitly,
one explicitly). In either case the result is the same.
std::string x = functionX();


No difference. Theoretically the copy constructor is called to initialize x with
the return value of functionX(). What happens inside functionX() is immaterial.
However, even in your case, the explicit conversion is identical to the implicit
one, there should be no difference.

In actuallity, the compiler may take buf and directly construct x from it.
Jul 19 '05 #2
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vo************@corp.supernews.com...
[...]
Both are acceptable to my compiler so obviously it is smart
enough to know I want buf translated to a string. However,
my question relates to what happens in this case:

std::string x = functionX();

In both cases the char* has to be converted to a string, but is
the copy constructor also called in both cases or is it smart
enough to call the conversion and assign it directly to string
'x'?
Depends on whether or not your compiler implements the RVO
or NRVO ([Named] Return Value Optimization).
Are there any unseen problems with either definition of
functionX?


No, because either way, a string is returned, whether by
constructing a temporary explicitly or doing so implicitly.
However, if your compiler implements the RVO, be aware
that you cannot rely on side effects from your copy c'tor,
since it might not get called as expected.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #3
David B. Held wrote:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vo************@corp.supernews.com...
[...]
Both are acceptable to my compiler so obviously it is smart
enough to know I want buf translated to a string. However,
my question relates to what happens in this case:

std::string x = functionX();

In both cases the char* has to be converted to a string, but is
the copy constructor also called in both cases or is it smart
enough to call the conversion and assign it directly to string
'x'?

Depends on whether or not your compiler implements the RVO
or NRVO ([Named] Return Value Optimization).

[snip] However, if your compiler implements the RVO, be aware
that you cannot rely on side effects from your copy c'tor,
since it might not get called as expected.


That is what I was hinting at, so doing it the second way might actually
be an optomization if your compiler supports it and so long as you are
aware of the copy ctor thing.

--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Jul 19 '05 #4

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

Similar topics

19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
4
by: Christopher Benson-Manica | last post by:
In a thread from substantially earlier this week, Harald van D?k <truedfx@gmail.comwrote: Being rather pendantic, I decided to try to verify whether this was true. I would appreciate...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
13
by: Urs Thuermann | last post by:
I wasn't able to find an answer to the following questions in a draft version of IOS 9899-99. What is the semantic when you decrement an unsigned int below 0 (or subtract two unsigned ints with...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...
0
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...

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.