26 Oct 2026
Value Categories Overloading
You are in for a bad time
ok but if
string & string::operator=(string &&other) {
string temp(other);
}
so this function binds to an assignment to an rval being passed in. but in the context of this function string &&other is nolonger an rval. its an rval reference. and an rval reference can be many things but at the end of the day an rval reference is an lval
If there's one thing to take away: "Value category is a quality of the expression". Not the object. The expression.
For example Data && x is an r-value reference. But that's its type. That's not the value category.
It has a name. We can change it over time. That makes its Value Category an l-value.
Entities have different Value Categories in different contexts.
Motivating Example
Take a look at the 4th line:
getData(42)returns aconst Dataprvalue.std::move(...)casts it into aconst Data&&xvalue.- The move constructor
Data(Data&&)cannot acceptconst Data&&, because that would discardconst. - The copy constructor
Data(const Data&)can bind toconst Data&&, so it gets selected.
Above, std::move() changes the value category but it retains the constness of the entity.
"Wtf Why"
An object has identity and stores a value. An expression is code that evaluates to a result; every expression has a type and a value category.
int x = 42;
x; // lvalue: identifies the existing object x
42; // prvalue: produces the integer value 42
std::move(x); // xvalue: identifies x, but treats it as expiring
The number 42 isn't intrinsically an rvalue. Lvalue, prvalue, and xvalue describe expressions, not the values inside objects. Both x and 42 can yield the same integer value while having different value categories.
A reference binds to an object: binding initializes a reference to refer to that object, without copying or moving it.
int x = 42;
int& a = x; // a binds to the existing object x
int&& b = 42; // a temporary int is materialized; b binds to it
int&& c = std::move(x); // c binds to x itself; no move takes place
And there's actually a set of rules to what expressions can bind to which values. All the source expressions are l values. B
| Source expression | T& |
const T& |
T&& |
const T&& |
|---|---|---|---|---|
| Non-const lvalue | ✓ | ✓ | ✗ | ✗ |
| Const lvalue | ✗ | ✓ | ✗ | ✗ |
| Non-const rvalue | ✗ | ✓ | ✓ | ✓ |
| Const rvalue | ✗ | ✓ | ✗ | ✓ |
Here, T is a non-const object type; rvalue includes prvalues and xvalues. A const T& can bind to temporaries, but a non-const T&& cannot bind to a const rvalue.
This table has a lot of implications. Firstly its that Data && a = b; will throw an error because T&& must bind to an r val.
Secondly, it also means
void foo (Data&& x) {
x = 42;
}
Data&& a = 42;
foo(a);
Won't compile.
a's type is Data&&. But that's not a's value category. a's value category is an lval. This means foo(Data&& x) can't bind to it as the r val ref x cannot bind to something that's an lval.
That table actually does a good job of explaining which function overloads bind to which inputs.
To determine which functions can and should bind to which data values, we just need to consult what function signatures are available, which references can bind to which values. and finally which of those references will preferentially bind to the value.
The original entity enters its new life in the function that takes it in. It may have had a certain value in the previous function a const/non-const l/r-value. That is its value. References can bind to certain values (consult table above). That's the logic for which function can be called.
Binding eligibility determines which overloads are possible but overload resolution determines which one is actually called. This helps us determine why copy constructors sometimes lose to move constructors (causing detriments to performance).
std::move()
std::move does not move anything. It casts an expression to an xvalue, preserving const:
Data d;
const Data cd;
std::move(d); // non-const xvalue; std::move returns Data&&
std::move(cd); // const xvalue; std::move returns const Data&&
One thing I would like to contrast is the following two lines of code
Data a;
Data&& ref = std::move(a); // reference binding only: no new Data object
Data b = foo(ref); //call 1, 2
Data b = foo(std::move(a)); //call 3, 4, 2
ref is an lvalue. And when passed in binds to the lval overloads. std::move() on the other hand truly redeems the Data object a as an expiring value. This makes it eligible for the move semantic call should we need it.
And as the moon rose higher the inessential houses began to melt away until gradually I became aware of the old island here that flowered once for Dutch sailors’ eyes—a fresh, green breast of the new world. Its vanished trees, the trees that had made way for Gatsby’s house, had once pandered in whispers to the last and greatest of all human dreams; for a transitory enchanted moment man must have held his breath in the presence of this continent, compelled into an aesthetic contemplation he neither understood nor desired, face to face for the last time in history with something commensurate to his capacity for wonder...
Perfect Forwarding
C++ has a reference collapse rule. Namely, it uses the type of the reference and the type of the entity.
Reference Collison Rule
| Combination | Result |
|---|---|
T& & |
T& |
T& && |
T& |
T&& & |
T& |
T&& && |
T&& |
& is infectious. A single l-val ref in either the LHS and the RHS causes the resultant expression to be a r-val ref.
Forwarding reference uses this to retain information about the original type of the
template<typename T>
void foo(T&& x) {
// ...
}
We call the input parameter a Forwarding Reference (rather than an R-value reference).
This will allow us to retrieve the initial type of the passed argument and pass it to the intended constructor, provided we do a bit of additional legwork...with forward
template<typename T>
void Wrapper(T&& x) {
Foo(std::forward<T>(x));
}
Wrapper(Data(42));
More generally,
| Original argument | Without std::forward |
With std::forward |
|---|---|---|
| Non-const lvalue | Non-const lvalue | Non-const lvalue |
| Const lvalue | Const lvalue | Const lvalue |
| Non-const rvalue | Non-const lvalue | Non-const xvalue |
| Const rvalue | Const lvalue | Const xvalue |
Without std::forward, everything passed through t becomes an lvalue expression, but its constness is preserved.
See that 6 was passed in became associated with t. t has a name. t is an lval. t as an lval will bind to the int&. Which is something we really did not intend.
You may be asking why we'd even need a forwarding function when it worked just fine previously.
The answer is convenience.
// One single template automatically handles (lvalue, lvalue), (rvalue, lvalue), etc.
template <typename T, typename... Args>
std::unique_ptr<T> create(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
std::string name = "Alice";
auto p1 = create<Person>(name, 30); // Forwards 'name' as lvalue (copies)
auto p2 = create<Person>(std::move(name), 30); // Forwards 'name' as rvalue (moves)
You do not need to write multiple functions or complex internal branch logic. You write your class constructor once (e.g., Person(std::string, int)), and library templates like std::make_unique or std::vector::emplace_back forward your arguments directly to that constructor in a single generic definition. Instead of manually writing 2^N overloads for every possible lvalue/rvalue parameter combination, the compiler instantiates the exact matching call signature behind the scenes.
The reason why is because Under C++'s "Rule of Zero," unless you define a custom destructor or copy/move operation, the compiler implicitly synthesizes:
- A default copy constructor (which member-wise copies every field).
- A default move constructor (which member-wise moves every field).
Because members like std::string or std::vector already implement their own move constructors, the compiler-generated move constructor for your class automatically invokes those existing move operations without you writing a single line of boilerplate.
A type describes what an expression represents (
int,const int, etc.). A reference type (int&,int&&) describes how a reference relates to an object. A value category describes how an expression produces or identifies its result. For example, givenint a = 42, the expressionsa,42, andstd::move(a)all have typeint, but their value categories are lvalue, prvalue, and xvalue, respectively.Now consider
template<typename T> void foo(T&& x). When callingfoo(a), the argument's lvalue category causesTto be deduced asint&. Reference collapsing then convertsT&&→int& &&→int&. When callingfoo(42), the argument's prvalue category causesTto be deduced asint, soT&&becomesint&&. Notice the order: value category influences type deduction; reference collapsing then determines the final reference type.
Other Stuff
I got a bit tired and I get the sense these aren't as important.
decay
std::decay strips a thing of its pointer and its cv designation so you can get the underlying type
decltype
decltype gives you the type including the value category. this is in contrast to std::decay
decltype(auto) a = foo(i); //compiler shorthand
decltype(foo(i)) a = foo(i);
//another way to use this
int && a = 42;
decltype(a) b = 42;
declval
takes an object and returns the reference type
typeid(std::declval<Type>()).name(); gives you type
i have no idea why you would try to do this
this
i really dont
#include <iostream>
#include <utility>
struct Type {
template <typename Self>
auto Foo(this Self&& self) {
if constexpr (std::is_rvalue_reference_v<Self&&>) {
std::cout << "Called on rvalue (can move from self)\n";
} else if constexpr (std::is_const_v<std::remove_reference_t<Self>>) {
std::cout << "Called on const lvalue\n";
} else {
std::cout << "Called on mutable lvalue\n";
}
}
};
int main() {
Type a;
const Type b;
a.Foo(); // Output: Called on mutable lvalue
b.Foo(); // Output: Called on const lvalue
Type{}.Foo(); // Output: Called on rvalue (can move from self)
std::move(a).Foo(); // Output: Called on rvalue (can move from self)
}