Search code examples
c++referencetype-systemsqualifierssubtyping

In C++, do ref-qualifiers express a form of subtyping?


From a programming language theory standpoint, in C++, qualifiers like const and volatile allow to express a form of subtyping, with for example int being a subtype of const int.

I was wondering if we could also consider that ref-qualifiers & and && allow to express a form of subtyping or not. In other words, can we consider that T, T& and T&& are related by a subtyping relationship or not, from a programming language theory standpoint? And if so, what is this relationship?


Solution

  • While you could consider CV-qualifiers to be "subtypes" under some definition, references are not. const T t = some_t; creates a new object of type T declared as const. You might think of it as creating a new const T, but either way, you are creating a new object whose value conceptually is a copy of an existing one.

    T &t = some_t; does not create a new object. It creates a reference to an existing object. That is a fundamentally different kind of thing in C++. References are not objects; the language is very clear about that. And it serves no useful purpose to think of a reference as a "subtype".