473,382 Members | 1,420 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,382 software developers and data experts.

TC++PL(se) section 7.3 Value Return

Hi everyone:

I am reading Bjarne Stroustrup's The C++ Programming
Language(Special Edition).

In section 7.3, bs wrote "Like the semantics of argument passing,
the semantics of function value return are identical to the semantics
of initialization. A return statement is considered to initialize an
unnamed variable of the returned type."

Is the *unnamed variable* here always create when we call a
function?

For instance, there is a function foo,

int foo(int i)
{
return i*i;
}

when we call it,

int i = foo(5);

Is the above statement mean the following code(not legal in c++)

int tmp = foo(5); // tmp here is the *unnamed variable* bs referred.
int i = tmp;

Even for the build-in types, it is ineffective, why need the *unnamed
variable*?

Regards.

Aug 12 '07 #1
4 1398
On 2007-08-12 15:48, Wayne Shu wrote:
Hi everyone:

I am reading Bjarne Stroustrup's The C++ Programming
Language(Special Edition).

In section 7.3, bs wrote "Like the semantics of argument passing,
the semantics of function value return are identical to the semantics
of initialization. A return statement is considered to initialize an
unnamed variable of the returned type."

Is the *unnamed variable* here always create when we call a
function?

For instance, there is a function foo,

int foo(int i)
{
return i*i;
}

when we call it,

int i = foo(5);

Is the above statement mean the following code(not legal in c++)

int tmp = foo(5); // tmp here is the *unnamed variable* bs referred.
int i = tmp;

Even for the build-in types, it is ineffective, why need the *unnamed
variable*?
No, there is something called the named return value optimisation, which
means that instead of creating an temporary and then copying it to the
variable the return value is created directly into the variable. The
following code should demonstrate it:

#include <iostream>

class Foo
{
int v;
public:
Foo(int i) : v(i)
{
std::cout << "Ctor\n";
}
Foo(const Foo& f) : v(f.v)
{
std::cout << "Copy Ctor\n";
}
Foo& operator=(const Foo& f)
{
v = f.v; std::cout << "Assign\n";
}
};

Foo bar(int i)
{
return Foo(i);
}

int main()
{
Foo f = bar(1);
}
If you only get the "Ctor" output the compiler has used the NRVO, if you
also get "Copy Ctor" then it has not.

--
Erik Wikström
Aug 12 '07 #2
On Aug 12, 3:48 pm, Wayne Shu <Wayne...@gmail.comwrote:
I am reading Bjarne Stroustrup's The C++ Programming
Language(Special Edition).
In section 7.3, bs wrote "Like the semantics of argument passing,
the semantics of function value return are identical to the semantics
of initialization. A return statement is considered to initialize an
unnamed variable of the returned type."
Is the *unnamed variable* here always create when we call a
function?
No. What do you think Bjarne meant by "is considered to
create", rather than just saying "creates"?

In general, the compiler can do anything it wants, as long as
the "observable behavior" of the program is not modified. In
this particular case, there is even special language to allow
the compiler to suppress the extra object even if doing so
changes the observable behavior.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 12 '07 #3
On Aug 12, 8:59 pm, James Kanze <james.ka...@gmail.comwrote:
On Aug 12, 3:48 pm, Wayne Shu <Wayne...@gmail.comwrote:

A return statement is considered to initialize an
unnamed variable of the returned type."
the return statement has to return the value in some unnamed variable
and initialize it with
the returned value, when we call the function in an expression or do
not catch the returned value explicitly in a variable.

That is why it is "A return statement is considered to initialize an
unnamed variable of the returned type." and not always "A return
statement creates and initialize an
unnamed variable of the returned type".

Aug 14 '07 #4
On Sun, 12 Aug 2007 14:06:52 +0000, Erik Wikström wrote:
On 2007-08-12 15:48, Wayne Shu wrote:
>Hi everyone:

I am reading Bjarne Stroustrup's The C++ Programming
Language(Special Edition).

In section 7.3, bs wrote "Like the semantics of argument passing,
the semantics of function value return are identical to the semantics
of initialization. A return statement is considered to initialize an
unnamed variable of the returned type."

Is the *unnamed variable* here always create when we call a
function?

For instance, there is a function foo,

int foo(int i)
{
return i*i;
}

when we call it,

int i = foo(5);

Is the above statement mean the following code(not legal in c++)

int tmp = foo(5); // tmp here is the *unnamed variable* bs referred.
int i = tmp;

Even for the build-in types, it is ineffective, why need the *unnamed
variable*?
Stroustrup describes how the program needs to _behave_. Compiler
implementers are free to achieve the same behaviour without introducing
an extra variable. For a built-in type no optimising compiler will use an
extra variable in your case.
No, there is something called the named return value optimisation, which
means that instead of creating an temporary and then copying it to the
variable the return value is created directly into the variable.
RVO allows to elide copy constructor calls even if the behaviour might be
different (for "odd" copy constructor implementations) which is never the
case for built-in types.
Foo bar(int i)
{
return Foo(i);
}
That is actually not RVO. That is a more general case of eliding copy
constructors for temporaries. RVO would look like this:

Foo bar(int i)
{
Foo foo(i);
// can do more stuff with foo here.
return foo;
}

--
Markus Schoder
Aug 15 '07 #5

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

Similar topics

5
by: torhu | last post by:
In 'The C++ Programming Language', Special Edition, there is an example in chapter 11.8, page 286 that is meant to illustrate the subscript operator (operator). It is a class that associates...
42
by: ES Kim | last post by:
Here's a code fragment from TC++PL Special Edition, p291: void f1(T a) { T v; T* p = &v; p--; // please note (my comment) *p = a; // oops: 'p' out of range, uncaught ++p; *p = a; // ok
5
by: Wayne Shu | last post by:
Now I'm reading Stroustrup's The C++ Programming Language(Special Edition). In section 4.4 Integer Types, he has wrote that "Using an unsigned instead of an int to gain one more bit to represent...
3
by: Guofu Chen | last post by:
Hi, In TC++PL, Chapter 10, there is an exercise which ask us to modify the following program: #include <iostream> int main() { std::cout << "Hello, world!/n" ;
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.