[cfe-dev] Function accepting rvalue and lvalue references

Howard Hinnant hhinnant at apple.com
Tue Oct 2 20:02:05 PDT 2012


On Oct 2, 2012, at 8:22 PM, James Dennett <james.dennett at gmail.com> wrote:

> On Tue, Oct 2, 2012 at 5:03 PM, Michael Lehn <michael.lehn at uni-ulm.de> wrote:
>> 
>> Assume I have some templated class
>> 
>>   template <typename T>
>>   struct Dummy {
>>       // ...
>>   };
>> 
>> and that I want to overload a fucntion 'foo' such that it accepts a lvalue or
>> rvalue reference of it.  I can do this using some 'IsDummy trait' as follows:
>> 
>>   template <typename A>
>>   std::enable_if<IsDummy<A>::value, void>
>>   foo(A &&dummy)
>>   {
>>       // ....
>>   }
>> 
>> So this is not the problem.  However, I somehow think that for some time a function
>> like
>> 
>>   template <typename A>
>>   void
>>   foo(Dummy<A> &&dummy)
>>   {
>>       // ....
>>   }
>> 
>> was capturing both, lvalue and rvalue reference.  Does my memory trick me?
> 
> Possibly so: it works for
>  template <typename A> void foo(A&&)
> because A can be deduced as an lvalue ref there (so that A&& is T& for
> some T), but not for your case.

Additional info for your sanity:

As originally proposed,

>>   template <typename A>
>>   void
>>   foo(Dummy<A> &&dummy)
>>   {
>>       // ....
>>   }

did bind to both lvalue and rvalue Dummy<A>.  Somewhere around 2009/2010 the committee changed the rules so that it would only bind to rvalue Dummy<A>.  You may have had experience with a compiler implementing the previous rules.

Howard




More information about the cfe-dev mailing list