Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   overload function for rvalue and lvalue references (http://www.velocityreviews.com/forums/t952948-overload-function-for-rvalue-and-lvalue-references.html)

Michael Lehn 10-02-2012 09:49 PM

overload function for rvalue and lvalue references
 
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 ...


Michael Lehn 10-03-2012 07:06 AM

Re: overload function for rvalue and lvalue references
 
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



SG 10-03-2012 08:59 AM

Re: overload function for rvalue and lvalue references
 
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

Michael Lehn 10-03-2012 10:05 AM

Re: overload function for rvalue and lvalue references
 
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




All times are GMT. The time now is 09:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.