c++0x and rvalue references on gcc 4.6.1

Kevin P. Fleming kpfleming@digium.com
Fri Sep 9 13:36:00 GMT 2011


On 09/09/2011 08:23 AM, leon zadorin wrote:
> 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...)

What you are missing is that the '&&' modifier for an argument to a 
*template* function has a different meaning than it does for a regular 
function.

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
Jabber: kfleming@digium.com | SIP: kpfleming@digium.com | Skype: kpfleming
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at www.digium.com & www.asterisk.org



More information about the Gcc-help mailing list