public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* A problem related to const rvalue
@ 2012-03-20  3:13 chmodexplorer
  2012-03-20 15:56 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: chmodexplorer @ 2012-03-20  3:13 UTC (permalink / raw)
  To: gcc

Hi,
I used gcc 4.8.0 to compile the piece of code:

#include <stdio.h>
#include <utility>

void f( const int & i )
{
        fprintf( stdout, "1 -> f( const int & i )-> %d\n", i );
}

void f( const int && i )
{
        fprintf( stdout, "2 -> f( const int && i )-> %d\n", i );
}

void f( int & i )
{
        fprintf( stdout, "3 -> f( int & i )-> %d\n", i );
}

void f( int && i )
{
        fprintf( stdout, "4 -> f( int && i )-> %d\n", i );
}

int f1() { return 0; }
int const f2() { return 0; }
int & f3( int & i ) { return i; }
int const & f4( const int & i ) { return i; }

int && f5( int && i ) { return std::move( i ); }
int const && f6( int const && i ) { return std::move( i ); }

int main()
{
        int i1 = 0;
        const int i2 = 0;

        int & i3 = i1;
        const int & i4 = i2;

        int && i5 = std::move( i1 );
        const int && i6 = std::move( i2 );


        f( i1 );
        f( i2 );
        f( i3 );
        f( i4 );
        f( i5 );
        f( i6 );

        f( f1() );
        f( f2() );
        f( f3( i3 ) );
        f( f4( i4 ) );
        f( f5( std::move( i1 ) ) );
        f( f6( std::move( i2 ) ) );

        return 0;
}

And the output of the program is:
3 -> f( int & i )-> 0
1 -> f( const int & i )-> 0
3 -> f( int & i )-> 0
1 -> f( const int & i )-> 0
3 -> f( int & i )-> 0
1 -> f( const int & i )-> 0
4 -> f( int && i )-> 0
4 -> f( int && i )-> 0
3 -> f( int & i )-> 0
1 -> f( const int & i )-> 0
4 -> f( int && i )-> 0
2 -> f( const int && i )-> 0

There are two lines of the strings: "4 -> f( int && i )-> 0". The first one is reasonable, but the second one makes me surprised. I guess that it should have been "2 -> f( const int && i )-> 0".

Is it a bug or by design? Who can answer the question for me?

Thanks very much!
cyril.

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

* Re: A problem related to const rvalue
  2012-03-20  3:13 A problem related to const rvalue chmodexplorer
@ 2012-03-20 15:56 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2012-03-20 15:56 UTC (permalink / raw)
  To: chmodexplorer; +Cc: gcc

2012/3/20  <chmodexplorer@sina.com>:
> Is it a bug or by design? Who can answer the question for me?

This list is for discussing the development of GCC not for help using
it, so this is the wrong mailing list for your question. It would be
more appropriate on the gcc-help mailing list, please take an
follow-up there, thanks.

I believe G++ is correct, the relevant text is in [basic.lval] where
the standard says that "Class prvalues can have cv-qualified types;
non-class prvalues always have cv-unqualified types."  The result of
the function f2() is a prvalue, so has type int, with no
const-qualification.

If you repeat the experiment with functions returning a class type
instead of int then you should see the behaviour you expect where f(
f2() ) will call f(const X&&)

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

end of thread, other threads:[~2012-03-20 15:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-20  3:13 A problem related to const rvalue chmodexplorer
2012-03-20 15:56 ` Jonathan Wakely

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).