C++ Primer example 7.2.2

A

arnuld

this is from C++ Primer 4/e example 7.2.2 on page 235 titled:
"References to const are more flexible"


#include <iostream>

/* increments the argument by 1 */
int incr( int& i)
{
++i;
}


int main()
{
short v1 = 0;
const int v2 = 42;

int v3 = incr(v1); /* error: must be error */
v3 = incr(v2); /* error: v2 is const */
v3 = incr(0); /* error: argument is rvalue */
v3 = incr( v1 + v2 );/* error: addition yields rvalue */

int v4 = incr(v3); /* OK: v3 is anon-const object of type int */
return 0;
}

/* OUTPUT
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra 7.2.2.cpp
7.2.2.cpp: In function 'int main()':
7.2.2.cpp:15: error: invalid initialization of reference of type
'int&' from expression of type 'short int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:16: error: invalid initialization of reference of type
'int&' from expression of type 'const int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:17: error: invalid initialization of non-const reference of
type 'int&' from a temporary of type 'int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:18: error: invalid initialization of non-const reference of
type 'int&' from a temporary of type 'int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:20: warning: unused variable 'v4'
[arnuld@arch cpp] $

*/


well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error

?
 
G

Gianni Mariani

arnuld said:
this is from C++ Primer 4/e example 7.2.2 on page 235 titled:
"References to const are more flexible"


#include <iostream>

/* increments the argument by 1 */
int incr( int& i)
{
++i;

.... did we miss a return expression here ?
}


int main()
{
short v1 = 0; ....
int v4 = incr(v3); /* OK: v3 is anon-const object of type int */
well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error

I don't understand why v3 is an anon const object.
 
F

Frank Birbacher

Hi!
/* increments the argument by 1 */
int incr( int& i)
{
++i;
}

And returns what?
int v4 = incr(v3); /* OK: v3 is anon-const object of type int */ [snip]
well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error

Well, I guess they are trying to say that "if v3 was initialised you
could use it as a parameter to incr and save the result of that call in v4".

Frank
 
F

Frank Birbacher

Hi!

Gianni said:
arnuld said:
int v4 = incr(v3); /* OK: v3 is anon-const object of type int */
[snip]
I don't understand why v3 is an anon const object.

Because it is not an anonymous const object, but "a non-const" object.
There is only a space missing in the code comment.

Frank
 

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

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top