Understanding the creature created by static_cast<type&&>(x)

Pages: 12
static_cast<int&&>(var) allows var to be treated as an rvalue reference to integer instead of an integer
Better now? :D Or can it be improved even more?

It is an int xvalue that refers to var. Just like how var is an int lvalue that refers to var. No references involved.

You can certainly rationalize both lvalue and xvalue expressions as some form of "references" (after all, they *reference* objects), and you're not the first to call them that way colloquially, but now you have *three* different kinds of references that have different properties, behavior, and usage: reference variables, reference types, and "reference" expressions! It's hard enough already.

I'm that kind of person that will never use something until he understands what it EXACTLY is and does.

What it *does* is instruct the compiler to make a call to the move constructor/move assignment/etc, so that Obj o1 = std::move(o2); can move if it can and copy if it can't. Everything else is boring details of how it was worked into the already complex specification of C++.
but now you have *three* different kinds of references that have different properties, behavior, and usage: reference variables, reference types, and "reference" expressions!


Where can I learn more about this?

reference variable: int& r = var; --- r is the reference variable

reference type: i suppose int&, float&, double& are reference types?

reference expression: mhm... no, never heard about that (AND I'M PRETTY SURE it has something to do with static_cast<&&>
reference expression: mhm... no, never heard about that

It does not exist. You just invented it by saying "static_cast<int&&>(var) allows var to be treated as an rvalue reference to integer instead of an integer".

What I am saying is that you're not the first person to invent such a thing in a conversation.
Oh... I understand haha

By the way the root of all this confusion is that in some cases, the language forces you to think about lvalues and rvalues in terms of references and vice versa. It kind of mixes the two concepts in different situations.

For example:

1
2
3
4
5
6
7
int var = 10;

// auto = int& because var is an lvalue
auto&& universal_reference = var;

// auto = int because int{5} is an rvalue
auto&& universal_reference2 = int{5};


Using the same symbols in different contexts to mean different things... it blows someone's mind sooner or later
Last edited on
Oh and... by the way... from what I've understood...

type&& inside of static_cast<type&&> has NOTHING TO DO with my intention of creating an xvalue.

It's just a way to tell the compiler to generate an xvalue for me, but it has NOTHING to do with rvalue references..............

(am i right?)

Last edited on
telling the compiler to generate an xvalue is exactly what it has to do with your intention of creating one.. That's how you express that intention in C++.
Topic archived. No new replies allowed.
Pages: 12