Re: Constrained Forwarding(R-Value Reference)
on Thu Feb 15 2007, pongba-AT-gmail.com wrote:
The current proposed wording addresses perfect forwarding as
following:
template< typename T>
void fo(T&& t); // T&& deduced as a l-value reference if the argument
is a l-value; r-value reference otherwise.
However, there might be some cases where we need **constrained**
forwarding, such as:
template<typename T>
void fo(MyC<T>&& t);
But in this case t would always be deduced as a r-value reference.
One alternative is to use traits:
template<typename T>
void fo(T&& t, is_my_c<T>::type* = 0);
This, despite being practical, is a little unwieldy.
Are there any better solutions?
I noticed that nobody seems to have answered your original question.
IIRC, we considered this class of cases and our first conclusion was
that we weren't sure there was a real use case for constrained
forwarding (do you have one)?
If I found a real use case, I'd apply enable_if
(http://www.boost.org/libs/utility/enable_if.html) or, better, concept
constraints
(http://www.generic-programming.org/languages/conceptcpp/), which is
much more general than constraining the arguments based on the mere
spellings of their types (i.e. "is the argument a specialization of
the MyC template?").
HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]