C
casul
Hi All,
Given the following code :
char const & identity( char const &c ) { return c; }
char const & f( ) { return identity('A'); }
char const & g( ) { return 'A'; }
Of course, the compiler complains of function 'g' returning a
reference to a temporary. But the compiler (I used gcc 4.2.x) doesn't
say anything regarding the function 'f'. I know that temporaries bound
to constant references have a life span that extends to the expression
in which the function call where the bounding took place is evaluated.
Now given that, is the fact of returning in turn the constant
reference returned by 'identity' considered to be part of the
expression in which 'identity' is evaluated, ie the expression is
return identity('A');
or does the evaluated expression just consist of the function call
'identity', ie:
identity('A');
?
I would have said the first case. But I'm not sure. I guess the main
question is: does a call to 'f' yield undefined behaviour ?
Thanks for your comments,
Olivier.
Given the following code :
char const & identity( char const &c ) { return c; }
char const & f( ) { return identity('A'); }
char const & g( ) { return 'A'; }
Of course, the compiler complains of function 'g' returning a
reference to a temporary. But the compiler (I used gcc 4.2.x) doesn't
say anything regarding the function 'f'. I know that temporaries bound
to constant references have a life span that extends to the expression
in which the function call where the bounding took place is evaluated.
Now given that, is the fact of returning in turn the constant
reference returned by 'identity' considered to be part of the
expression in which 'identity' is evaluated, ie the expression is
return identity('A');
or does the evaluated expression just consist of the function call
'identity', ie:
identity('A');
?
I would have said the first case. But I'm not sure. I guess the main
question is: does a call to 'f' yield undefined behaviour ?
Thanks for your comments,
Olivier.