overload function for rvalue and lvalue references

M

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

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 ...
 
M

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

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/arch...-references-c-0x-features-in-vc10-part-2.aspx
 
S

SG

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/wg21/docs/papers/2008/n2812.html

and

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


Cheers!
SG
 
M

Michael Lehn

Am Dienstag, 2. Oktober 2012 23:54:55 UTC+2 schrieb Michael Lehn:

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.)



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


See

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

and

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


Cheers!
SG

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

Cheers,

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top