I looked recently at this video explaining the ideas of concepts lite in C++, which are likely to appear this year as a TS. Now, I also learned about universal references/forwarding references (as described here) and that T&& can have two meanings depending on the context (i.e. if type deduction is being performed or not). This leads naturally to the question how concepts will interact with universal references?
To make it concrete, in the following example we have
void f(int&& i) {}
int i = 0;
f(i); // error, looks for f(int&)
f(0); // fine, calls f(int&&)
and
template <typename T>
void f(T&& test) {}
int i = 0;
f(i); // fine, calls f(T&&) with T = int& (int& && = int&)
f(0); // fine, calls f(T&&) with T = int&& (int&& && = int&&)
But what happens if we use concepts?
template <typename T>
requires Number<T>
void f(T&& test) {}
template <Number T>
void g(T&& test) {}
void h(Number&& test) {}
int i = 0;
f(i); // probably should be fine?
f(0); // should be fine anyway
g(i); // probably also fine?
g(0); // fine anyway
h(i); // fine or not?
h(0); // fine anyway
Especially the last example bothers me a bit, since there are two conflicting
principles. First, a concept used in this way is supposed to work just as a type and second, if T is a deduced type, T&& denotes a universal reference instead of an rvalue reference.
Thanks in advance for clarification on this!
It all depends on how the concept itself is written. Concepts-Lite itself (latest TS as of this writing) is agnostic on the matter: it defines mechanisms by which concepts may be defined and used in the language, but does not add stock concepts to the library.
On the other hand document N4263 Toward a concept-enabled standard library is a declaration of intent by some members of the Standard Committee that suggests the natural step after Concepts-Lite is a separate TS to add concepts to the Standard Library with which to constrain e.g. algorithms.
That TS may be a bit far down along the road, but we can still take a look at how concepts have been written so far. Most examples I’ve seen somewhat follow a long tradition where everything revolves around a putative, candidate type that is usually not expected to be a reference type. For instance some of the older Concepts-Lite drafts (e.g. N3580) mention concepts such as Container which have their roots in the SGI STL and survive even today in the Standard Library in the form of 23.2 Container requirements.
A telltale pre-forwarding reference sign is that associated types are described like so:
Value type X::value_type The type of the object stored in a container. The value type must be Assignable, but need not be DefaultConstructible.
If we translate this naïvely to Concepts-Lite, it could look like:
template<typename X>
concept bool Container = requires(X x) {
typename X::value_type;
// other requirements...
};
In which case if we write
template<typename C>
requires Container<C>
void example(C&& c);
then we have the following behavior:
std::vector<int> v;
// fine
// this checks Container<std::vector<int>>, and finds
// std::vector<int>::value_type
example(std::move(v));
// not fine
// this checks Container<std::vector<int>&>, and
// references don't have member types
example(v);
There are several ways to express the value_type requirement which handles this situation gracefully. E.g. we could tweak the requirement to be typename std::remove_reference_t<X>::value_type instead.
I believe the Committee members are aware of the situation. E.g. Andrew Sutton leaves an insightful comment in a concept library of his that showcases the exact situation. His preferred solution is to leave the concept definition to work on non-reference types, and to remove the reference in the constraint. For our example:
template<typename C>
// Sutton prefers std::common_type_t<C>,
// effectively the same as std::decay_t<C>
requires<Container<std::remove_reference_t<C>>>
void example(C&& c);
T&& always has the same "meaning" -- it is an rvalue reference to T.
The interesting thing happens when T itself is a reference. If T=X&&, then T&& = X&&. If T=X& then T&& = X&. The rule that an rvalue reference to an lvalue reference is an lvalue reference is what allows the forwarding reference technique to exist. This is called reference collapsing1.
So as for
template <typename T>
requires Number<T>
void f(T&& test) {}
this depends on what Number<T> means. If Number<T> permits lvalue references to pass, then that T&& will work like a forwarding reference. If not, T&& it will only bind to rvalues.
As the rest of the examples are (last I checked) defined in terms of the first example, there you have it.
There may be additional "magic" in the concepts specification, but I am not aware of it.
1 There is never actually a reference-to-a-reference. In fact, if you type int y = 3; int& && x = y; that is an illegal expression: but using U = int&; U&& x = y; is perfectly legal, as reference collapsing occurs.
An analogy to how const works sometimes helps. If T const x; is const regardless of if T is const. If T_const is const, then T_const x; is also const. And T_const const x; is const as well. The constness of x is the max of the constness of the type T and any "local" modifiers.
Similarly, the lvalue-ness of a reference is the max of the lvalue-ness of the T and any "local" modifiers. Imagine if the language had two keywords, ref and lvalue. Replace & with lvalue ref and && with ref. The use of lvalue without ref is illegal under this translation..
T&& means T ref. If T was int lvalue ref, then reference collapsing results in int lvalue ref ref -> int lvalue ref, which translates back as int&. Similarly, T& translates to int lvalue ref lvalue ref -> int lvalue ref, and if T=int&&, then T& translates to int ref lvalue ref -> int lvalue ref -> int&.
This is a difficult thing. Mostly, when we write concepts, we want to focus on the type definition (what can we do with T) and not its various forms (const T, T&, T const&, etc). What you generally ask is, "can I declare a variable like this? Can I add these things?". Those questions tend to be valid irrespective of references or cv-qualifications. Except when they aren't.
With forwarding, template argument deduction frequently gives you those over forms (references and cv-qualified types), so you end up asking questions about the wrong types. sigh. What to do?
You either try to define concepts to accommodate those forms, or you try to get to the core type.
Related
To make a concept checking if a type can be converted without narrowing to another, it is proposed here to make it using std::forward and std::type_identity_t like this:
template<class T, class U>
concept __construct_without_narrowing = requires (U&& x) {
{ std::type_identity_t<T[]>{std::forward<U>(x)} } -> T[1];
};
I understand from it why something like this:
To{std::declval<From>()}
gives incorrect results, but when i try to simplify it using another idea in the paper, writing just
template <typename From, typename To>
concept WithoutNarrowing =
requires (From x) {
{(To[1]){x}}
->std::same_as<To[1]>;
};
It seems to give the same results. What circumstances have to occur for it to give different result? Or is it equivalent? For what reason is std::forward used here?
This is the usual approach for type traits like this that involve some kind of function/constructor argument.
U is the type from which T is supposed to be constructed, but if we want to discuss the construction we also need to consider the value category of the argument. It may be an lvalue or a rvalue and this can affect e.g. which constructor is usable.
The idea is that we map the rvalue argument case to a non-reference U or rvalue reference U and the lvalue argument case to a lvalue reference U, matching the mapping of expressions in decltype and of return types with value categories in function call expressions.
Then, by the reference collapsing rules, U&& will be a lvalue reference if the constructor argument is a lvalue and otherwise a rvalue reference. Then using std::forward means that the actual argument we give to the construction will indeed be a lvalue argument when U was meant to represent one and a rvalue argument otherwise.
Your approach using {(To[1]){x}} doesn't use the forwarding and so would always only test whether construction from a lvalue can be done without narrowing, which is not what is expected if e.g. U is a non-reference.
Your approach is further incorrect because (To[1]){x} is not valid syntax in standard C++. If X is a type you can have X{x} or (X)x, but not (X){x}. The last syntax is part of C however and called a compound literal there. For that reason a C++ compiler may support it as an extension to C++. That's why the original implementation uses the round-about way with std::type_identity_t.
The implementation seems to also be written for an earlier draft of C++20 concepts. It is now not possible to use types to the right of -> directly for a requirement. Instead a concept, i.e. -> std::same_as<T[1]>, must be used as in your suggested implementation.
well there is difference between (U u), (U& u) and (U&& u) that std::forward is supposed to preserve. in case of (U u) the type has to have defined a copy constructor (since (U u) basically means "pass a copy of")
This is an rvalue reference:
void foo(int&& a);
It does not bind to lvalues:
int i = 42;
foo(i); // error
This is a universal reference:
template<typename T>
void bar(T&& b);
It binds to rvalues and it also binds to lvalues:
bar(i); // okay
This is an rvalue reference:
template<typename T>
struct X
{
void baz(T&& c);
};
It does not bind to lvalues:
X<int> x;
x.baz(i); // error
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes like T&&&, T&*, T# or T&42 (just kidding on that last one)? If so, what were the reasons for rejecting alternative syntaxes?
A universal reference such as T&& can deduce T to be an "object type", or a "reference type"
In your example it can deduce T as int when passed an rvalue, so the function parameter is int&&, or it can deduce T as int& when passed an lvalue, in which case the function parameter is int& (because the reference collapsing rules say std::add_rvalue_reference<int&>::type is just int&)
If T isn't deduced by the function call (as in your X::baz example) then it can't be deduced to int&, so the reference isn't a universal reference.
So IMHO there's really no need for new syntax, it fits nicely into template argument deduction and reference collapsing rules, with the small tweak that a template parameter can be deduced as a reference type (where in C++03 a function template parameter of type T or T& would always deduce T as an object type.)
These semantics and this syntax were proposed right from the beginning when rvalue references and a tweak to the argument deduction rules were proposed as the solution to the forwarding problem, see N1385. Using this syntax to provide perfect forwarding was proposed in parallel with proposing rvalue references for the purposes of move semantics: N1377 was in the same mailing as N1385. I don't think an alternative syntax was ever seriously proposed.
IMHO an alternative syntax would actually be more confusing anyway. If you had template<typename T> void bar(T&#) as the syntax for a universal reference, but the same semantics as we have today, then when calling bar(i) the template parameter T could be deduced as int& or int and the function parameter would be of type int& or int&& ... neither of which is "T&#" (whatever that type is.) So you'd have grammar in the language for a declarator T&# which is not a type that can ever exist, because it actually always refers to some other type, either int& or int&&.
At least with the syntax we've got the type T&& is a real type, and the reference collapsing rules are not specific to function templates using universal references, they're completely consistent with the rest of the type system outside of templates:
struct A {} a;
typedef A& T;
T&& ref = a; // T&& == A&
Or equivalently:
struct A {} a;
typedef A& T;
std::add_rvalue_reference<T>::type ref = a; // type == A&
When T is an lvalue reference type, T&& is too. I don't think a new syntax is needed, the rules really aren't that complicated or confusing.
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes...
Yes, it is confusing, IMO (I'll disagree with #JonathanWakely here). I remember that during an informal discussion (lunch, I think) about the early design of the overall feature we did discuss different notations (Howard Hinnant and Dave Abrahams were there bringing their idea and the EDG guys were giving feedback on how it could be fit in the core language; this predates N1377). I think I remember &? and &|&& were considered, but all this was verbal; I'm not aware of meeting notes having been taken (but I believe this is also when John suggested the use of && for rvalue references). Those were the early stages of the design, however, and there were plenty of fundamental semantic issues to consider at the time. (E.g., during that same lunch discussion we also raised the possibility of not having two kinds of references, but instead having two kinds of reference parameters.)
A more recent aspect of the confusion this causes is found in the C++17 feature of "class template argument deduction" (P0099R3). There a function template signature is formed by transforming the signature of constructors and constructor templates. For something like:
template<typename T> struct S {
S(T&&);
};
a function template signature
template<typename T> auto S(T&&)->S<T>;
is formed to use for the deduction of a declaration like
int i = 42;
S s = i; // Deduce S<int> or S<int&>?
Deducing T = int& here would be counter-intuitive. So we're having to add a "special deduction rule to disable the special deduction rule" in this circumstance :-(
As a developer with only a few years of C++ experience, I must agree that universal references are confusing. It seems to me that this stuff is completely impossible to understand, let alone actively use without reading Scott Meyers and/or watching the relevant talks on YouTube.
It is not just that && can be either an r-value reference or a "universal reference", it is the way templates are used to distinguish the types of reference. Imagine you are a normal engineer working in software development. You read something like:
template <typename T>
void foo (T&& whatever) {...}
and then the function body of foo tells you that the input parameter must be a very specific class type that has been defined in an in-house library. Wonderful, you think, the template is obsolete in the current software version, probably its a leftover from previous developments, so let's get rid of it.
Good luck with the compiler errors…
Nobody who hasn't learned this explicitly would assume that you'd implement foo as a function template here just to employ template deduction rules and reference collapsing rules which happen to coincide so that you end up with what is called "perfect forwarding". It really looks more like a dirty hack than anything else. I think creating a syntax for universal references would have made things simpler, and a developer who used to work on an older code base would at least know that there is something that has to be googled here. Just my two cents.
This is an rvalue reference:
void foo(int&& a);
It does not bind to lvalues:
int i = 42;
foo(i); // error
This is a universal reference:
template<typename T>
void bar(T&& b);
It binds to rvalues and it also binds to lvalues:
bar(i); // okay
This is an rvalue reference:
template<typename T>
struct X
{
void baz(T&& c);
};
It does not bind to lvalues:
X<int> x;
x.baz(i); // error
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes like T&&&, T&*, T# or T&42 (just kidding on that last one)? If so, what were the reasons for rejecting alternative syntaxes?
A universal reference such as T&& can deduce T to be an "object type", or a "reference type"
In your example it can deduce T as int when passed an rvalue, so the function parameter is int&&, or it can deduce T as int& when passed an lvalue, in which case the function parameter is int& (because the reference collapsing rules say std::add_rvalue_reference<int&>::type is just int&)
If T isn't deduced by the function call (as in your X::baz example) then it can't be deduced to int&, so the reference isn't a universal reference.
So IMHO there's really no need for new syntax, it fits nicely into template argument deduction and reference collapsing rules, with the small tweak that a template parameter can be deduced as a reference type (where in C++03 a function template parameter of type T or T& would always deduce T as an object type.)
These semantics and this syntax were proposed right from the beginning when rvalue references and a tweak to the argument deduction rules were proposed as the solution to the forwarding problem, see N1385. Using this syntax to provide perfect forwarding was proposed in parallel with proposing rvalue references for the purposes of move semantics: N1377 was in the same mailing as N1385. I don't think an alternative syntax was ever seriously proposed.
IMHO an alternative syntax would actually be more confusing anyway. If you had template<typename T> void bar(T&#) as the syntax for a universal reference, but the same semantics as we have today, then when calling bar(i) the template parameter T could be deduced as int& or int and the function parameter would be of type int& or int&& ... neither of which is "T&#" (whatever that type is.) So you'd have grammar in the language for a declarator T&# which is not a type that can ever exist, because it actually always refers to some other type, either int& or int&&.
At least with the syntax we've got the type T&& is a real type, and the reference collapsing rules are not specific to function templates using universal references, they're completely consistent with the rest of the type system outside of templates:
struct A {} a;
typedef A& T;
T&& ref = a; // T&& == A&
Or equivalently:
struct A {} a;
typedef A& T;
std::add_rvalue_reference<T>::type ref = a; // type == A&
When T is an lvalue reference type, T&& is too. I don't think a new syntax is needed, the rules really aren't that complicated or confusing.
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes...
Yes, it is confusing, IMO (I'll disagree with #JonathanWakely here). I remember that during an informal discussion (lunch, I think) about the early design of the overall feature we did discuss different notations (Howard Hinnant and Dave Abrahams were there bringing their idea and the EDG guys were giving feedback on how it could be fit in the core language; this predates N1377). I think I remember &? and &|&& were considered, but all this was verbal; I'm not aware of meeting notes having been taken (but I believe this is also when John suggested the use of && for rvalue references). Those were the early stages of the design, however, and there were plenty of fundamental semantic issues to consider at the time. (E.g., during that same lunch discussion we also raised the possibility of not having two kinds of references, but instead having two kinds of reference parameters.)
A more recent aspect of the confusion this causes is found in the C++17 feature of "class template argument deduction" (P0099R3). There a function template signature is formed by transforming the signature of constructors and constructor templates. For something like:
template<typename T> struct S {
S(T&&);
};
a function template signature
template<typename T> auto S(T&&)->S<T>;
is formed to use for the deduction of a declaration like
int i = 42;
S s = i; // Deduce S<int> or S<int&>?
Deducing T = int& here would be counter-intuitive. So we're having to add a "special deduction rule to disable the special deduction rule" in this circumstance :-(
As a developer with only a few years of C++ experience, I must agree that universal references are confusing. It seems to me that this stuff is completely impossible to understand, let alone actively use without reading Scott Meyers and/or watching the relevant talks on YouTube.
It is not just that && can be either an r-value reference or a "universal reference", it is the way templates are used to distinguish the types of reference. Imagine you are a normal engineer working in software development. You read something like:
template <typename T>
void foo (T&& whatever) {...}
and then the function body of foo tells you that the input parameter must be a very specific class type that has been defined in an in-house library. Wonderful, you think, the template is obsolete in the current software version, probably its a leftover from previous developments, so let's get rid of it.
Good luck with the compiler errors…
Nobody who hasn't learned this explicitly would assume that you'd implement foo as a function template here just to employ template deduction rules and reference collapsing rules which happen to coincide so that you end up with what is called "perfect forwarding". It really looks more like a dirty hack than anything else. I think creating a syntax for universal references would have made things simpler, and a developer who used to work on an older code base would at least know that there is something that has to be googled here. Just my two cents.
I have a code that operates on a vector:
template<typename T>
void doVector(vector<T>& v, T&& value) {
//....
v.push_back(value);
//...
}
For normal push_back, do I need to use forward(value), move(value) or just value (according to new C++11) ? and how do they impact the performance?
For example,
v.push_back(forward<T>(value));
The current code will not compile when the second argument is lvalue because T&& will turn out to be X& which means T needs to be X& which in turn means std::vector<T> will become std::vector<X&> which will NOT match the first argument which is std::vector<X> &. Hence, it is an error.
I would use two template parameters:
template<typename T, typename V>
void doVector(vector<T> & v, V && value)
{
v.emplace_back(std::forward<V>(value));
}
Since V could a different type from T, so emplace_back makes more sense, because not only it solves the problem, it makes the code more generic. :-)
Now the next improvement : since we're using emplace_back which creates the object of type T from argument value (possibly using constructor), we could take advantage of this fact, and make it variadic function:
template<typename T, typename ...V>
void doVector(vector<T> & v, V && ... value)
{
v.emplace_back(std::forward<V>(value)...);
}
That is even more generic, as you could use it as:
struct point
{
point(int, int) {}
};
std::vector<point> pts;
doVector(pts, 1, 2);
std::vector<int> ints;
doVector(ints, 10);
Hope that helps.
forward(value) is used if you need perfect forwarding meaning, preserving things like l-value, r-value.
forwarding is very useful because it can help you avoid writing multiple overloads for functions where there are different combinations of l-val, r-val and reference arguments
move(value) is actually a type of casting operator that casts an l-value to an r-value
In terms of performances both avoid making extra copies of objects which is the main benefit.
So they really do two different things
When you say normal push_back, I'm not sure what you mean, here are the two signatures.
void push_back( const T& value );
void push_back( T&& value );
the first one you can just pass any normal l-val, but for the second you would have to "move" an l-val or forward an r-val. Keep in mind once you move the l-val you cannot use it
For a bonus here is a resource that seems to explain the concept of r-val-refs and other concepts associated with them very well.
As others have suggested you could also switch to using emplace back since it actually perfect forwards the arguments to the constructor of the objects meaning you can pass it whatever you want.
When passing a parameter that's a forwarding reference, always use std::forward.
When passing a parameter that's an r-value, use std::move.
E.g.
template <typename T>
void funcA(T&& t) {
funcC(std::forward<T>(t)); // T is deduced and therefore T&& means forward-ref.
}
void funcB(Foo&& f) {
funcC(std::move(f)); // f is r-value, use move.
}
Here's an excellent video by Scott Meyers explaining forwarding references (which he calls universal references) and when to use perfect forwarding and/or std::move:
C++ and Beyond 2012: Scott Meyers - Universal References in C++11
Also see this related question for more info about using std::forward: Advantages of using forward
http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Scott-Meyers-Universal-References-in-Cpp11
That video, IMO, has the best explanation of when to use std::forward and when to use std::move. It presents the idea of a Universal Reference which is IMO an extremely useful tool for reasoning about move semantics.
This is an rvalue reference:
void foo(int&& a);
It does not bind to lvalues:
int i = 42;
foo(i); // error
This is a universal reference:
template<typename T>
void bar(T&& b);
It binds to rvalues and it also binds to lvalues:
bar(i); // okay
This is an rvalue reference:
template<typename T>
struct X
{
void baz(T&& c);
};
It does not bind to lvalues:
X<int> x;
x.baz(i); // error
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes like T&&&, T&*, T# or T&42 (just kidding on that last one)? If so, what were the reasons for rejecting alternative syntaxes?
A universal reference such as T&& can deduce T to be an "object type", or a "reference type"
In your example it can deduce T as int when passed an rvalue, so the function parameter is int&&, or it can deduce T as int& when passed an lvalue, in which case the function parameter is int& (because the reference collapsing rules say std::add_rvalue_reference<int&>::type is just int&)
If T isn't deduced by the function call (as in your X::baz example) then it can't be deduced to int&, so the reference isn't a universal reference.
So IMHO there's really no need for new syntax, it fits nicely into template argument deduction and reference collapsing rules, with the small tweak that a template parameter can be deduced as a reference type (where in C++03 a function template parameter of type T or T& would always deduce T as an object type.)
These semantics and this syntax were proposed right from the beginning when rvalue references and a tweak to the argument deduction rules were proposed as the solution to the forwarding problem, see N1385. Using this syntax to provide perfect forwarding was proposed in parallel with proposing rvalue references for the purposes of move semantics: N1377 was in the same mailing as N1385. I don't think an alternative syntax was ever seriously proposed.
IMHO an alternative syntax would actually be more confusing anyway. If you had template<typename T> void bar(T&#) as the syntax for a universal reference, but the same semantics as we have today, then when calling bar(i) the template parameter T could be deduced as int& or int and the function parameter would be of type int& or int&& ... neither of which is "T&#" (whatever that type is.) So you'd have grammar in the language for a declarator T&# which is not a type that can ever exist, because it actually always refers to some other type, either int& or int&&.
At least with the syntax we've got the type T&& is a real type, and the reference collapsing rules are not specific to function templates using universal references, they're completely consistent with the rest of the type system outside of templates:
struct A {} a;
typedef A& T;
T&& ref = a; // T&& == A&
Or equivalently:
struct A {} a;
typedef A& T;
std::add_rvalue_reference<T>::type ref = a; // type == A&
When T is an lvalue reference type, T&& is too. I don't think a new syntax is needed, the rules really aren't that complicated or confusing.
Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes...
Yes, it is confusing, IMO (I'll disagree with #JonathanWakely here). I remember that during an informal discussion (lunch, I think) about the early design of the overall feature we did discuss different notations (Howard Hinnant and Dave Abrahams were there bringing their idea and the EDG guys were giving feedback on how it could be fit in the core language; this predates N1377). I think I remember &? and &|&& were considered, but all this was verbal; I'm not aware of meeting notes having been taken (but I believe this is also when John suggested the use of && for rvalue references). Those were the early stages of the design, however, and there were plenty of fundamental semantic issues to consider at the time. (E.g., during that same lunch discussion we also raised the possibility of not having two kinds of references, but instead having two kinds of reference parameters.)
A more recent aspect of the confusion this causes is found in the C++17 feature of "class template argument deduction" (P0099R3). There a function template signature is formed by transforming the signature of constructors and constructor templates. For something like:
template<typename T> struct S {
S(T&&);
};
a function template signature
template<typename T> auto S(T&&)->S<T>;
is formed to use for the deduction of a declaration like
int i = 42;
S s = i; // Deduce S<int> or S<int&>?
Deducing T = int& here would be counter-intuitive. So we're having to add a "special deduction rule to disable the special deduction rule" in this circumstance :-(
As a developer with only a few years of C++ experience, I must agree that universal references are confusing. It seems to me that this stuff is completely impossible to understand, let alone actively use without reading Scott Meyers and/or watching the relevant talks on YouTube.
It is not just that && can be either an r-value reference or a "universal reference", it is the way templates are used to distinguish the types of reference. Imagine you are a normal engineer working in software development. You read something like:
template <typename T>
void foo (T&& whatever) {...}
and then the function body of foo tells you that the input parameter must be a very specific class type that has been defined in an in-house library. Wonderful, you think, the template is obsolete in the current software version, probably its a leftover from previous developments, so let's get rid of it.
Good luck with the compiler errors…
Nobody who hasn't learned this explicitly would assume that you'd implement foo as a function template here just to employ template deduction rules and reference collapsing rules which happen to coincide so that you end up with what is called "perfect forwarding". It really looks more like a dirty hack than anything else. I think creating a syntax for universal references would have made things simpler, and a developer who used to work on an older code base would at least know that there is something that has to be googled here. Just my two cents.