return values and type conversions

N

Noah Roberts

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
 
D

David B. Held

Noah Roberts said:
[...]
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
 
R

Ron Natalie

Noah Roberts said:
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.
 
N

Noah Roberts

David said:
[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top