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
?
"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
?