A reference is a new name for a pre-existing object, and acts as if it was the referenced object. It is not an object itself, as crazy as that sounds. They're usually implemented with either pointers or renaming under the hood, depending on the use case. (Which means that they might or might not actually exist in memory, and thus have "indeterminate" size that is usually either sizeof(T&) == 0 or sizeof(T&) == sizeof(T*).)
// Simple example. Should be an internal copy-paste.
int i = 5;
int& ref = i;
ref = 3; // This should compile to the same machine code as...
i = 3; // this.
// -----
// "Complex" example. Will nearly always be a pointer with non-pointer syntax.
void func(int& ir) { ir = 2; }
int i = 5;
func(i);
Generally, a reference can be used just like a pointer, except with a few caveats. It must be assigned on creation and can never be reassigned, it can't do pointer arithmetic, it can't be null, and it uses non-pointer syntax.
They follow pointer cv-ness rules, with the caveat that all references are inherently cv T* const-like. (Since they can't be reassigned.) You can make a reference const and/or volatile to adjust how the object is viewed through the reference, without affecting the object's cv-ness.
Normal references have no effect on an object's lifetime. Because of this, they must bind to something that has permanence, known as an "lvalue".
int i = 6;
int& r = i; // Valid. Binds to i.
int& boom = 0; // Invalid. Cannot bind to temporary.
This can be dangerous, if the lvalue is deallocated or otherwise invalidated between the time the reference is assigned and the time it's accessed.
int& dangly() { int i = 3; return i; }
int& ohno = dangly(); // i is destroyed at the semicolon.
int boom = ohno; // The object we're copying doesn't exist here.
Rvalue references can bind to temporary objects, known as "rvalues", and extend the temporary's lifetime until the reference dies.
int&& rr = 3;
int i = 5 + rr; // That random 3 is still alive here.
const references can bind to either lvalue or rvalue, and extend the object's lifetime until the reference dies, but at the cost of being read-only.
int i = 4;
const int& ri = i; const int& rr = 8;
assert(rr == ri * 2);
Member functions can be ref-qualified; similar to cv-qualifying a member function, this allows you to specify whether a function can be called on temporary or non-temporary objects.
Rvalue syntax is also used for "perfect forwarding", which is a special rule that allows a reference parameter taken by a template function to be either lvalue or rvalue as necessary. This looks like an rvalue reference parameter, but it's not.
template<typename T>
void func(T&& t) { std::cout << t << '\n'; }
int i = 1; func(i); // Calls func<int>(int&).
func(3.14); // Calls func<float>(float&&).
There are special rules about where they can or cannot be used, so you sometimes see them wrapped in a class like std::reference_wrapper. (The easiest example is that you can't create an array of references, because of the "not an object" and "indeterminate size" things.)
int a, b, c, d, e;
int& arr[] = { a, b, c, d, e }; // Error.
std::reference_wrapper<int> matey[] = { a, b, c, d, e }; // Good.
There's probably other stuff, but that's all that comes to mind at the moment.
View all comments
Show parent comment
3
u/conundorum 17d ago
A reference is a new name for a pre-existing object, and acts as if it was the referenced object. It is not an object itself, as crazy as that sounds. They're usually implemented with either pointers or renaming under the hood, depending on the use case. (Which means that they might or might not actually exist in memory, and thus have "indeterminate" size that is usually either
sizeof(T&) == 0orsizeof(T&) == sizeof(T*).)Generally, a reference can be used just like a pointer, except with a few caveats. It must be assigned on creation and can never be reassigned, it can't do pointer arithmetic, it can't be null, and it uses non-pointer syntax.
They follow pointer cv-ness rules, with the caveat that all references are inherently
cv T* const-like. (Since they can't be reassigned.) You can make a referenceconstand/orvolatileto adjust how the object is viewed through the reference, without affecting the object's cv-ness.Normal references have no effect on an object's lifetime. Because of this, they must bind to something that has permanence, known as an "lvalue".
This can be dangerous, if the lvalue is deallocated or otherwise invalidated between the time the reference is assigned and the time it's accessed.
Rvalue references can bind to temporary objects, known as "rvalues", and extend the temporary's lifetime until the reference dies.
constreferences can bind to either lvalue or rvalue, and extend the object's lifetime until the reference dies, but at the cost of being read-only.Member functions can be ref-qualified; similar to cv-qualifying a member function, this allows you to specify whether a function can be called on temporary or non-temporary objects.
Rvalue syntax is also used for "perfect forwarding", which is a special rule that allows a reference parameter taken by a template function to be either lvalue or rvalue as necessary. This looks like an rvalue reference parameter, but it's not.
There are special rules about where they can or cannot be used, so you sometimes see them wrapped in a class like
std::reference_wrapper. (The easiest example is that you can't create an array of references, because of the "not an object" and "indeterminate size" things.)There's probably other stuff, but that's all that comes to mind at the moment.