public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/47026] New: invalid temporary is being assigned to a const-reference
@ 2010-12-20 11:30 tmoschou at gmail dot com
  2010-12-20 11:33 ` [Bug c++/47026] " tmoschou at gmail dot com
  2010-12-20 12:03 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: tmoschou at gmail dot com @ 2010-12-20 11:30 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: invalid temporary is being assigned to a
                    const-reference
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: tmoschou@gmail.com


Created attachment 22830
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22830
Prog which demonstrates bug. returns EXIT_FAILURE for g++, EXIT_SUCCESS for
MSVC++

The following program (also attached) highlights an obvious bug in g++. When
compiled in Microsoft Visual C++, the program executes as expected: ‘ptr ==
alias’. But when compiled with g++: ‘ptr != alias’. I believe that this is
because a temporary is wrongly created in the statement,

const int* const &alias(prt);
or alternatively
const int* const &alias = ptr;

A ‘const int*’ temp is created from ‘ptr’ of type ‘int*’. So ‘alias’ is a
reference to the temp and not ‘ptr’. I’ve tried using a ‘const_cast’ operator
but with no luck for the desired results.

Program was compiled with the command: g++ -o prog.exe prog.cpp

program: prog.cpp
---------------<start>--------------
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

    int a = 10;
    int* ptr = &a;

    /* temporary is being assigned to a const-reference : BAD */
    const int* const &alias(ptr);

    ptr = NULL; /* or ptr = 0; */

    if (ptr != alias) { 
        /* should never execute but DOES on g++ (not on MSVC++)*/
        cout << "ptr != alias" << endl;
        return (EXIT_FAILURE);
    }

    cout << "ptr == alias" << endl;
    return (EXIT_SUCCESS);

}
---------------<end>---------------


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

* [Bug c++/47026] invalid temporary is being assigned to a const-reference
  2010-12-20 11:30 [Bug c++/47026] New: invalid temporary is being assigned to a const-reference tmoschou at gmail dot com
@ 2010-12-20 11:33 ` tmoschou at gmail dot com
  2010-12-20 12:03 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: tmoschou at gmail dot com @ 2010-12-20 11:33 UTC (permalink / raw)
  To: gcc-bugs

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

Terry <tmoschou at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tmoschou at gmail dot com

--- Comment #1 from Terry <tmoschou at gmail dot com> 2010-12-20 11:33:37 UTC ---
Whoops, forgot to say compiled on Ubuntu 10.10 x64.


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

* [Bug c++/47026] invalid temporary is being assigned to a const-reference
  2010-12-20 11:30 [Bug c++/47026] New: invalid temporary is being assigned to a const-reference tmoschou at gmail dot com
  2010-12-20 11:33 ` [Bug c++/47026] " tmoschou at gmail dot com
@ 2010-12-20 12:03 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2010-12-20 12:03 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-12-20 12:03:12 UTC ---
(In reply to comment #0)
> 
>     /* temporary is being assigned to a const-reference : BAD */
>     const int* const &alias(ptr);

This is the correct behaviour, see 12.2 [class.temporary] paragraphs 4 and 5 in
the standard:

"4 There are two contexts in which temporaries are destroyed at a different
point than the end of the full-expression. ...
"5 The second context is when a reference is bound to a temporary. The
temporary to which the reference is bound or the temporary that is the complete
object to a subobject of which the temporary is bound persists for the lifetime
of the reference except as specified below."

The lifetime of the temporary ends on return from main, so the temporary also
ends when main returns.


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

end of thread, other threads:[~2010-12-20 12:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-20 11:30 [Bug c++/47026] New: invalid temporary is being assigned to a const-reference tmoschou at gmail dot com
2010-12-20 11:33 ` [Bug c++/47026] " tmoschou at gmail dot com
2010-12-20 12:03 ` redi 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).