This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
A problem related to const rvalue
- From: <chmodexplorer at sina dot com>
- To: "" <gcc at gcc dot gnu dot org>
- Date: Tue, 20 Mar 2012 11:13:21 +0800
- Subject: A problem related to const rvalue
- Reply-to: chmodexplorer at sina dot com
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.