0

The MS C++ Docs specify that const types are distinct types from their non-const counter parts. i.e. const int is a completely different type from int, with its own set of rules for how an object of that type can be manipulated. This idea is further supported by the ability to cast e.g. const int to int in the same way you can cast char to int.

In that vein, are references (and pointers for that matter) to types themselves distinct types or are they "built upon" their "underlying type", if that is a fair way to say it.

  • 2
    *"themselves distinct types or are they 'built upon' their 'underlying type'"* -- you present these options as if they are mutually exclusive. Why not be both, much like `const int` is a distinct type built upon `int`? – JaMiT Sep 17 '22 at 12:16
  • Claiming that references are not distinct types would ignore the fact that functions taking the type itself, a reference to the type or a pointer to a type are considered different overloads and behave different. However a reference to a type allows for the same operations a named variable of the type does. As for pointers to a type: those use different semantics. You usually won't be able to apply the `->` operator of the unary `*` to a non-pointer type... – fabian Sep 17 '22 at 12:20
  • `typeid` slices the reference information. Probably for good reasons, although I'm a bit surprised the core language does not provide a non-slicing version. You can use `boost::typeindex::type_id_with_cvr` as an alternative that doesn't slice that type information. – Eljay Sep 17 '22 at 12:54
  • Related: [**A:** What does "cv-unqualified" mean in C++?](https://stackoverflow.com/a/15413274) – JaMiT Sep 17 '22 at 13:23
  • 1
    An interesting thing about references is how they came to be part of the language, and answer the question "Why is `this` a self-pointer instead of a self-reference?" For the latter, `this` predates *references* being part of the language. For the former, Bjarne didn't like `int add(int *a, int *b) { return *a + *b; }` so created aliasing *reference types* `int add(int& a, int& b) { return a + b; }` to make the syntax cleaner. They're almost syntactic sugar... but they're really semantic sugar. And the parms ought to be `int const&`. – Eljay Sep 17 '22 at 14:18
  • If you're asking if a "reference to an `int`" something distinct in the C++ type system from a "reference to a " then the answer is "yes". – Peter Sep 17 '22 at 14:23

0 Answers0