public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34075]  New: temporary used in ?: expression tho second and third expr. lvalues
@ 2007-11-12 18:09 james dot kanze at gmail dot com
  2007-11-19  3:57 ` [Bug c++/34075] " pinskia at gcc dot gnu dot org
  2009-12-08 22:28 ` [Bug c++/34075] [DR 587] " redi at gcc dot gnu dot org
  0 siblings, 2 replies; 4+ messages in thread
From: james dot kanze at gmail dot com @ 2007-11-12 18:09 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2176 bytes --]

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/34075] temporary used in ?: expression tho second and third expr. lvalues
  2007-11-12 18:09 [Bug c++/34075] New: temporary used in ?: expression tho second and third expr. lvalues james dot kanze at gmail dot com
@ 2007-11-19  3:57 ` pinskia at gcc dot gnu dot org
  2009-12-08 22:28 ` [Bug c++/34075] [DR 587] " redi at gcc dot gnu dot org
  1 sibling, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-11-19  3:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-11-19 03:57 -------
t.cc:28: warning: returning reference to temporary


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34075


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/34075] [DR 587] temporary used in ?: expression tho second and third expr. lvalues
  2007-11-12 18:09 [Bug c++/34075] New: temporary used in ?: expression tho second and third expr. lvalues james dot kanze at gmail dot com
  2007-11-19  3:57 ` [Bug c++/34075] " pinskia at gcc dot gnu dot org
@ 2009-12-08 22:28 ` redi at gcc dot gnu dot org
  1 sibling, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu dot org @ 2009-12-08 22:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from redi at gcc dot gnu dot org  2009-12-08 22:28 -------
C::dflt has type const int by pd->d[i] has type int, so they do not have the
same type and the warning is correct.  This is DR 587 which was just changed in
the latest draft
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#587


-- 

redi at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2009-12-08 22:28:20
               date|                            |
            Summary|temporary used in ?:        |[DR 587] temporary used in
                   |expression tho second and   |?: expression tho second and
                   |third expr. lvalues         |third expr. lvalues


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34075


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/34075] [DR 587] temporary used in ?: expression tho second and third expr. lvalues
       [not found] <bug-34075-4@http.gcc.gnu.org/bugzilla/>
@ 2014-10-02  9:37 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-10-02  9:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34075

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a dup of 51317 which has now been fixed on the trunk.

*** This bug has been marked as a duplicate of bug 51317 ***


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-10-02  9:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-12 18:09 [Bug c++/34075] New: temporary used in ?: expression tho second and third expr. lvalues james dot kanze at gmail dot com
2007-11-19  3:57 ` [Bug c++/34075] " pinskia at gcc dot gnu dot org
2009-12-08 22:28 ` [Bug c++/34075] [DR 587] " redi at gcc dot gnu dot org
     [not found] <bug-34075-4@http.gcc.gnu.org/bugzilla/>
2014-10-02  9:37 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).