Re: why std::vector<T>&& is not a universal reference?
Am 09.10.2012 00:48, schrieb Gennadiy Rozental:
Using Scott Meyers terminology, why std::vector<T>&& (or for that
matter any other template) is not considered a universal reference?
Thus I am required to provide 2 overloads to "fake" perfect
forwarding. Things quickly become unmanageable once you move toward
functions with multiple arguments (I do realize the same issue exist
for any concrete type, but I hoped templates would behave better).
Any reason why is that?
Rvalue references were added to the language to allow specifying
function signatures
- that accept only rvalues
- that accept only lvalues
- that accept both (The "perfect forwarding" case)
If you would make every occurrence of && in a signature to perfect
forwarding, then there is no direct way to realize the first bullet.
Perfect forwarding - while of-course an important thing to have - was
still considered as a special case compared to the first two items.
An alternative choice that would have solved the problem you mention
would have been to introduce three different declaration forms, e.g.
T&&& for perfect forwarding, but at the time where the current state
was decided for, this idea wasn't considered worth the effort it
seemed to cause and now its too late to speculate upon that further.
Any workarounds?
The usual technique is to specify a function of the form T&& and to
constrain it accordingly. E.g.
#include <vector>
#include <type_traits>
template<class T>
struct is_vector : std::false_type {};
template<class T>
struct is_vector<std::vector<T>> : std::true_type {};
template<class T>
typename std::enable_if<is_vector<
typename std::remove_cv<typename std::remove_reference<T>::type>::type,
/ReturnType/>::value
>::type
foo(T&&){ /.../ }
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]