0

Hi i am reading about auto and const in C++ and came to know about 2 examples. The 2 examples are as follows:

auto &h = 42;

const auto &j = 42;

Now my questions are as follows:

  1. Why in the 1st example a plain reference cannot be bind to a literal and this example fails. I think it is because a reference is itself not an object it is just another name for an object and since the literal 42 does not exist as an object in memory so the 1st example fails. Is this correct?
  2. How does the second example works? I mean since a reference is itself not an object but since the 2nd example is working does this mean that the reference variable j is created as an object?
  3. So if the 2nd example is working does this mean in future if i want to create a variable using int i=0; instead can i create it using const auto &i=0 ? Will this have the same affect as int i=0?
Jason
  • 36,170
  • 5
  • 26
  • 60

1 Answers1

3
  1. Why in the 1st example a plain reference cannot be bind to a literal and this example fails.

Because the rules of the language say that reference to non-const cannot bind to an rvalue. And a literal is a prvalue.

  1. How does the second example works?

A temporary object is materialised from the prvalue, the reference is bound to that temporary object and the lifetime of the temporary is extended to match the lifetime of the reference.

  1. i want to create a variable using int i=0; instead can i create it using const auto &i=0 ?

Note that you couldn't modify the temporary object through the const reference, while you can modify int. As such, these aren't quite the same. Also, as a member variable or a parameter, these would differ significantly.

In some cases you could, but you shouldn't because it's unnecessarily complicated. Remember that you were confused about the code. You wont be the last. Prefer writing programs that don't confuse others. If you want to create a variable using int i=0;, then use int i=0;.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    "because the language says it" is right. But feels unsatisfactory to me. It might be worth mentioning that with a reference to a literal, you could change the literal. Which is problematic. However, a const reference doesn't allow modifying the literal, which removes the problem. Call it speculation, or illustration, if you want. But at least it illustrates a potential reason "why". – Jeffrey Feb 05 '21 at 13:57
  • 1
    @Jeffrey I'm not satisfied with that reasoning, since a reference to const doesn't refer to the literal either, so why would the reference to non-cost refer to the literal? – eerorika Feb 05 '21 at 14:02
  • 1
    @Jeffrey For comparison, Rust allows `f(&mut 5)` which makes a mutable (non-const) reference to the `5`. I think it's a matter of design choice. After all, C++ treats `const auto &x = prvalue()` roughly as `auto y = prvalue(); const auto &x=y` extending the lifetime by materializing the temporary. C++ could do this even in the non-`const` case, in principle, and be closer to what Rust does. Neither design choice seems to be necessarily better from all perspectives (IMO). – chi Feb 05 '21 at 14:49
  • 1
    Fair points. I see the "because the standard says so" indeed hides much complexity and discussions :-) – Jeffrey Feb 05 '21 at 15:22