This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/34075] New: temporary used in ?: expression tho second and third expr. lvalues
- From: "james dot kanze at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 12 Nov 2007 18:09:07 -0000
- Subject: [Bug c++/34075] New: temporary used in ?: expression tho second and third expr. lvalues
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Note: I get the same error on i686-pc-linux-gnu.
The easiest is to give a short example:
--------- reftmp.cc ----------
typedef int S ;
// struct S { int i ; } ;
class C
{
public:
struct D { S d[ 64 ] ; } ;
S const& g( int i ) const ;
private:
D* pD ;
S dflt ;
S const& g1( D* pd, int i ) const ;
} ;
S const&
C::g( int i ) const
{
return g1( pD, i ) ;
}
S const&
C::g1( D* pd, int i ) const
{
return pd == 0 ? dflt : pd->d[ i ] ;
}
int
main()
{
C aC ;
aC.g( 1 ) ;
return 0 ;
}
--------- reftmp.cc ----------
When compiled without any options, this warns about returning a
reference to a temporary in the line:
return pd == 0 ? dflt : pd->d[ i ] ;
And in fact, the generated code does create a temporary and
return a reference to a temporary. (According to the standard,
if the second and third expressions in a ?: are both lvalues,
and have the same type, then the results are an lvalue.)
If I replace the typedef in the first line with the commented
out struct, then the code works correctly. (In the actual code,
the class C is a template, and I can't control how the user
instantiates it.)
For the moment, as a work-around, I've replaced the line in
question with:
return *(pd == 0 ? &dflt : &pd->d[ i ]) ;
This works, and generates the code which should have been
generated by the original line.
--
James Kanze (GABI Software) email:james.kanze@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
--
Summary: temporary used in ?: expression tho second and third
expr. lvalues
Product: gcc
Version: 4.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: james dot kanze at gmail dot com
GCC build triplet: sparc-sun-solaris2.8
GCC host triplet: sparc-sun-solaris2.8
GCC target triplet: sparc-sun-solaris2.8
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34075