This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
c++0x and rvalue references on gcc 4.6.1
- From: leon zadorin <leonleon77 at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 9 Sep 2011 23:23:19 +1000
- Subject: c++0x and rvalue references on gcc 4.6.1
Hi everyone.
Just started looking-into/reading-on c++0x rvalue refs and would like
clarification on the following code being compiled by gcc v 4.6.1 (on
freebsd 8.2 release, amd64):
#include <iostream>
struct init {};
struct x {
template <typename InitPolicy>
void
init_from(InitPolicy const & x)
{
::std::cout << "by ref\n";
}
template <typename InitPolicy>
void
init_from(InitPolicy && x)
{
::std::cout << "by rvalue ref possibly stealing resources held by x\n";
}
};
int
main()
{
x x1;
init i;
x1.init_from(i);
return 0;
}
compiled with:
c++ -std=c++0x main.cc
produces:
"by rvalue ref possibly stealing resources held by x"
>From various experiments, the above 'x1.init_from(i)' binds as
'x1.init_from<init&>(i)' [as opposed to 'x1.init_from<init>(i)]... but
even if so -- I was wondering how does this correlate with the
following quote from wiki on c++11:
http://en.wikipedia.org/wiki/C%2B%2B0x#Rvalue_references_and_move_constructors
"... A named variable will never be considered to be an rvalue even if
it's declared as such; in order to get an rvalue, the function
template std::move<T>() should be used ..."
I am most likely missing something simple here (just starting to read
no rval refs...)
Best regards
Leon Zadorin.