Why auto&& can bind lvalue while int&& cannot? [duplicate] - c++

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.

Related

What is the difference between normal rvalue reference and template rvalue reference? [duplicate]

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.

What is the purpose of std::forward()'s rvalue reference overload?

I'm experimenting with Perfect Forwarding and I found that
std::forward() needs two overloads:
Overload nr. 1:
template <typename T>
inline T&& forward(typename
std::remove_reference<T>::type& t) noexcept
{
return static_cast<T&&>(t);
}
Overload nr.2:
template <typename T>
inline T&& forward(typename
std::remove_reference<T>::type&& t) noexcept
{
static_assert(!std::is_lvalue_reference<T>::value,
"Can not forward an rvalue as an lvalue.");
return static_cast<T&&>(t);
}
Now a typical scenario for Perfect Forwarding is something like
template <typename T>
void wrapper(T&& e)
{
wrapped(forward<T>(e));
}
Of course you know that when wrapper() is instantiated, T depends on whether the argument passed to it is an lvalue or an rvalue. If it's an lvalue of type U, T is deduced to U&. If it's an rvalue, T is deduced to U.
In any case - in the scope of wrapper() - e is an lvalue, therefore it always uses the first overload of std::forward().
Now my question:
What is a valid scenario in which the 2nd overload is used (and is needed)?
The design rationale for forward is discussed in great detail in N2951.
This document lays out 6 use cases:
A. Should forward an lvalue as an lvalue. All implementations pass
this test. But this is not the classic perfect forwarding pattern. The
purpose of this test is to show that implementation 2 fails in its
stated goal of preventing all use cases except perfect forwarding.
B. Should forward an rvalue as an rvalue. Like use case A, this is
an identity transformation and this presents a motivating example
where the identity transformation is needed.
C. Should not forward an rvalue as an lvalue. This use case
demonstrates a dangerous situation of accidentally creating a dangling
reference.
D. Should forward less cv-qualified expressions to more
cv-qualified expressions. A motivating use case involving the
addition of const during the forward.
E. Should forward expressions of derived type to an accessible,
unambiguous base type. A motivating use case involving forwarding a
derived type to a base type.
F. Should not forward arbitrary type conversions. This use case
demonstrates how arbitrary conversions within a forward lead to
dangling reference run time errors.
The second overload enables cases B and C.
The paper goes on to provide examples of each use case, which are too lengthy to be repeated here.
Update
I've just run the "solution" of just the first overload through these 6 use cases, and this exercise shows that the second overload also enables use case F: Should not forward arbitrary type conversions.

How will concepts lite interact with universal references?

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.

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&&).
Thus,to allow only rvalues, one need to write
template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes>
void foo(T&& x) {}
and not,
template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes>
void foo(T&& x) {}
My question is , why for forwarding references, rvalues are deduced to be of type T and not T&& ? I guess, if they are deduced as T&& then also same referencing collapsing rule works as T&& && is same as T&&.
Because at the time, deducing rvalue A arguments as A&& instead of A was seen as an unnecessary complication and departure from the normal rules of deduction:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm
We really didn't even know if we could get one exception of the deduction rules (for the lvalue A case), and it never even occurred to us that we would dare ask for two exceptions. To do so, the benefit would have had to have been: It makes what is impossible, possible.
After all, without the single special deduction rule for the lvalue case, perfect forwarding is impossible, as N1385 so aptly demonstrated.
Even with today's hindsight, adding another special deduction rule so that the client can avoid having to negate a template constraint, does not seem like a very high benefit/cost ratio. Especially compared to the benefit/cost ratio we were shooting for in 2002.

Syntax for universal references

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.