Value category of register in C++

123 Views Asked by At

I have come across the keyword register in C++ and I am curious about its value category. Are the variables created with the register keyword lvalue (because they can be referenced) or rvalue (because they are stored in a processor register and cannot be accessed by a pointer to their address in memory)?

I've studied the basic category values at the link above on the cppreference, but I don't understand where register variables go according to these definitions.

3

There are 3 best solutions below

0
On BEST ANSWER

Are the variables created with the register keyword lvalue (because they can be referenced) or rvalue (because they are stored in a processor register and cannot be accessed by a pointer to their address in memory)?

The question is categorically mistaken and doesn't make any sense. Variables do not have the property "value category".

"Value category" is a property of expressions. For example an id-expression naming a variable is an lvalue expression, no matter what kind of variable that is. register doesn't change anything about that. Variables or objects however do not have a value category.

It is also wrong that only lvalues can be referenced. This again is a categorical error, because references are bound to objects, not expressions. And any object can be referenced by a reference, although certain kinds of references allow only certain value categories for their initializing expressions.

In the same way pointers do not point to expression, but to objects, and again, every object can be pointed to.

It is true that the built-in & operator can't be applied to rvalue expressions, but that doesn't prevent taking e.g. the address of a temporary object. It only (intentionally) makes it a bit harder to do so. Because an id-expression naming a variable is always an lvalue expression, built-in & can always be applied to them. The register keyword doesn't affect this at all.

The register keyword (before its deprecation and removal from the language) had no function except to hint at the compiler that it might be better to keep that variable in a register. It didn't restrict the use of the variable declared register in any way, nor did it force the compiler to actually keep the object in a register. Because compilers have been clever enough for a long time to decide themselves which objects should be held in registers, there isn't any practical purpose for register anymore.

3
On

Have you searched for register on cppreference? It says it clearly: the keyword is unused since C++17. Prior to this, it was only a hint (probably ignored since a long time ago) for the compiler, as you can see here.

0
On

register is deprecated or removed depending on the c++ standard version.

If it wasn't deprecated then it'd be similar to volatile which alters how the compiler treats the variable but it doesn't change its value category. You can have a volatile lvalue, rvalue etc.