Mes
Mes

Reputation: 347

Is Expression type same as object, references, or functions type?

n4868(6.8.1) :

[Note 1: [basic.types] and the subclauses thereof impose requirements on implementations regarding the representation of types. There are two kinds of types: fundamental types and compound types. Types describe objects, references, or functions. — end note]

It said "Types describe objects, reference, or functions.", but how about Expression? There is also a label called "Type" in "Properties of expressions" in spec(n4868 7.2.2). Is the "Type" same as "Type" in Basic(n4868 6.8)?

Sorry about that this is kind of a tricky question, but I'm confused about that.

Upvotes: 5

Views: 192

Answers (2)

user12002570
user12002570

Reputation: 1

In C++,

the type of an expression is never a reference.

Lets discuss this with the following snippet:

int i = 0; //i is of type int
int &refI = i; 
int &&ref = 3;

Case 1

Here we consider the statement:

int i = 0;

In the above statement, the variable i is an lvalue(which is a value category of expressions) of type int.

Case 2

Here we consider the statement:

int &refI = i;

In the above statement, refI has lvalue reference type. But note that when used as/in an expression, the expression refI is an lvalue of the type that this reference refers to(which is nothing but int here).

That is, for a reference variable1 refI, the expression refI has the referenced type.

Case 3

Here we consider the statement:

int &&ref = 3;

In the above statement, ref has rvalue reference type. Again when used as/in an expression, the expression ref is an lvalue of the type that this reference refers to(which is int).

Thus we again see that for a reference variable1 ref, the expression ref has the referenced type.


Is the "Type" same as "Type" in Basic(n4868 6.8)? It said "Types describe objects, reference, or functions.", but how about Expression?

Type has the same meaning. The above example snippet and explanation should clarify the expression part that you're asking.


Summary

Say we have a reference variable r that has either an lvalue reference type or an rvalue reference type. Then regardless, the expression r will always be an lvalue of the type that the reference refers to.


1Note that a reference is not an object by itself. Instead a reference refers to some other object.

Upvotes: 4

Sneftel
Sneftel

Reputation: 41454

“Expression” is a grammatical construct. The string 3+4 parses as an expression, as does foo(bar). Each of those expressions, when evaluated following the rules of the language, will result in a value, which will have a type. Informally one could call that the type of the expression itself, and I believe the standard does just that. But there’s no Expression type.

Upvotes: 3

Related Questions