user2953119
user2953119

Reputation:

Formal differences T, T& and T&&

I'm looking for a normative reference to the Standard.

We can declare a variable of reference type as follows:

int a = 5;
int &b = a;

Formally, the type of a is int, but the type of b is int&. So why can we initialize int& with int? I was looking for a standard conversion for these types, but it seems there isn't.

Upvotes: 2

Views: 85

Answers (1)

Brian Bi
Brian Bi

Reputation: 119457

You can initialize int& with int because the standard says:

A variable declared to be a T& or T&&, that is, “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T.

(N3936, 8.5.3/1)

There is no "standard conversion" from T to T&, and

When a parameter of reference type binds directly (8.5.3) to an argument expression, the implicit conversion sequence is the identity conversion, unless the argument expression has a type that is a derived class of the parameter type, in which case the implicit conversion sequence is a derived-to-base Conversion (13.3.3.1).

(N3936, 13.3.3.1.4/1; emphasis mine)

Upvotes: 4

Related Questions