Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overload function for rvalue and lvalue references

Reply
Thread Tools

overload function for rvalue and lvalue references

 
 
Michael Lehn
Guest
Posts: n/a
 
      10-02-2012
Assume I have some templated class

template <typename T>
struct Dummy {
// ...
};

And 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)
{
// ....
}

If I recall correctly some C++11 draft allowed that

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

would accept both, lvalue- and value- references.

Now my questions:
a) Is it correct that it was allowed in some draft?
b) Why was this feature/bug removed?

I think I read somewhere that it was due to a conflict with "concepts".
However, I can't find it anymore ...

 
Reply With Quote
 
 
 
 
Michael Lehn
Guest
Posts: n/a
 
      10-03-2012
On 2012-10-02 21:49:55 +0000, Michael Lehn said:

> Assume I have some templated class
>
> template <typename T>
> struct Dummy {
> // ...
> };
>
> And 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)
> {
> // ....
> }
>
> If I recall correctly some C++11 draft allowed that
>
> template <typename A>
> foo(Dummy<A> &&dummy)
> {
> // ....
> }
>
> would accept both, lvalue- and value- references.
>
> Now my questions:
> a) Is it correct that it was allowed in some draft?
> b) Why was this feature/bug removed?
>
> I think I read somewhere that it was due to a conflict with "concepts".
> However, I can't find it anymore ...


Ok, I am actually not totally insane:

http://blogs.msdn.com/b/vcblog/archi...10-part-2.aspx


 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      10-03-2012
Am Dienstag, 2. Oktober 2012 23:54:55 UTC+2 schrieb Michael Lehn:
>
> Assume I have some templated class
>
> template <typename T>
> struct Dummy {
> // ...
> };
>
> And 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)
> {
> // ....
> }


Or alternativly

template<class A>
void foo(Dummy<A> & x)
{
// ....
}

template<class A>
inline void foo(Dummy<A> && x)
{
return foo(x);
}

But one wonders why you would want to do that. (!)

(Note: the template argument deduction that makes perfect forwarding
work does not apply here.)


> If I recall correctly some C++11 draft allowed that
>
> template <typename A>
> foo(Dummy<A> &&dummy)
> {
> // ....
> }
>
> would accept both, lvalue- and value- references.
>
> Now my questions:
> a) Is it correct that it was allowed in some draft?


Yes. Up until about three years ago lvalues were allowed to bind to
rvalue references. Now, they won't do that anymore. Note that this
does not contradict perfect forwarding since what makes perfect
forwarding work is
(1) a special template argument deduction rule
(2) reference collapsing

> b) Why was this feature/bug removed?


See

"A Safety Problem with RValue References (and what to do about it)"
http://www.open-std.org/jtc1/sc22/wg...008/n2812.html

and

"Fixing a Safety Problem with Rvalue References:
Proposed Wording (Revision 1)"
http://www.open-std.org/jtc1/sc22/wg...009/n2844.html


Cheers!
SG
 
Reply With Quote
 
Michael Lehn
Guest
Posts: n/a
 
      10-03-2012
On 2012-10-03 08:59:25 +0000, SG said:

> Am Dienstag, 2. Oktober 2012 23:54:55 UTC+2 schrieb Michael Lehn:
>>
>> Assume I have some templated class
>>
>> template <typename T>
>> struct Dummy {
>> // ...
>> };
>>
>> And 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)
>> {
>> // ....
>> }

>
> Or alternativly
>
> template<class A>
> void foo(Dummy<A> & x)
> {
> // ....
> }
>
> template<class A>
> inline void foo(Dummy<A> && x)
> {
> return foo(x);
> }
>
> But one wonders why you would want to do that. (!)
>
> (Note: the template argument deduction that makes perfect forwarding
> work does not apply here.)
>
>
>> If I recall correctly some C++11 draft allowed that
>>
>> template <typename A>
>> foo(Dummy<A> &&dummy)
>> {
>> // ....
>> }
>>
>> would accept both, lvalue- and value- references.
>>
>> Now my questions:
>> a) Is it correct that it was allowed in some draft?

>
> Yes. Up until about three years ago lvalues were allowed to bind to
> rvalue references. Now, they won't do that anymore. Note that this
> does not contradict perfect forwarding since what makes perfect
> forwarding work is
> (1) a special template argument deduction rule
> (2) reference collapsing
>
>> b) Why was this feature/bug removed?

>
> See
>
> "A Safety Problem with RValue References (and what to do about it)"
> http://www.open-std.org/jtc1/sc22/wg...008/n2812.html
>
> and
>
> "Fixing a Safety Problem with Rvalue References:
> Proposed Wording (Revision 1)"
> http://www.open-std.org/jtc1/sc22/wg...009/n2844.html
>
>
> Cheers!
> SG


Perfect! Thanks a lot for these references. That's exactly what I was
looking for!

Cheers,

Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversionsare not applied to the left expressions"? Lighter C++ 6 06-21-2007 04:38 PM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
lvalue (s) and rvalue (s) jimjim C++ 7 03-26-2006 06:37 PM
const and non-const operator[] Vs. lvalue and rvalue use Mark Stijnman C++ 2 04-22-2005 02:32 PM



Advertisments