-5

Consider this toy example:

struct A { int x; };
int main() {
    A&& a=A(); 
    return 0;
}
  1. Is there an implicit cast from temporary values (values of built-in type, and temporary objects) to objects of type rvalue reference?

  2. Does a has an address?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

1 Answers1

2
  1. Is there an implicit cast from temporary values (values of built-in type, and temporary objects) to objects of type rvalue reference?

First a nitpick, casts are explicit conversions by definition, so there cannot be an implicit cast. Beyond that, a reference is distinctly not an object type. It may even not require any storage. References are bound to objects. It'd be more appropriate to think about them as alternative names of objects. Rvalue references in particular can only bind to objects of a particular sort. Which are roughly all "nameless" and "temporary" values. We may also cast to an rvalue reference to force an object being designated as "about to expire".

There's however an intricate point here, once an rvalue reference binds to an object, it's no longer "nameless". In particular, this means that by using that reference we call an object "by name" and thus obtain an lvalue. Furthermore, and to coincide with that, binding a reference in block scope to a temporary, prolongs the lifetime of the temporary until the end of the scope. And that brings us to the next point.

  1. Does a has an address?

Yes and no. The refernece itself may or may not require storage, as I said previously. But that's moot, since it's just the name of an object. Taking the address of a reference will yield the address of the object that was bound to it. In this case, it will be the address of the temporary bound to it.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Thanks, but I think I didn't get you completely. Do you mean that the compiler binds `a` to `A()` without any sort of cast? Then when should I use explicit cast (`static_cast(..)`)? Only for lvalue expressions? – Stav Alfi Dec 25 '17 at 16:52
  • @StavAlfi - The operand of the cast will usually be an lvalue. The point of the cast is essentially just to allow another rvalue reference to bind to that object (for instance, the reference parameter of a move constructor). And since the cast is quite a lot to type, there's the common `std::move` utility that does this very cast. – StoryTeller - Unslander Monica Dec 25 '17 at 16:54
  • And yes, the compiler will bind some references even if you don't cast. Depends on how the types align according to the type system. – StoryTeller - Unslander Monica Dec 25 '17 at 16:55
  • Okay, Thanks! Can you please provide examples for automatic bind: "the compiler will bind some references even if you don't cast". – Stav Alfi Dec 25 '17 at 21:27
  • @StavAlfi - Your post already has one. `A&& a=A();`. Even `A a; A& aa = a;` is an example. If you are learning C++ from trial and error, I advise against it. [There are better ways](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Dec 26 '17 at 06:23