0

Re-seating a reference in C++ is explicitly disallowed as a design decision. But why is it not a hard compiler error? Would it complicate the language to make this an error?

  • 6
    The language syntax doesn't have any possibility for reseating a reference – M.M Apr 08 '17 at 02:48
  • 4
    Compiler designers did better than making it a hard compiler error: they made it syntactically impossible. – Sergey Kalinichenko Apr 08 '17 at 02:48
  • It may be syntactically meaningless to try to reseat a reference, but sometimes it's useful to take advantage of the "const" property of a reference; but this hides logic bugs since it is not a hard error. – user7835540 Apr 08 '17 at 02:50
  • @user7835540 You have `const` pointers and smart-pointers for that, though. – Dai Apr 08 '17 at 02:52
  • @Dai In the case where type deduction is involved, pointer dereferencing can make the code harder to read. – user7835540 Apr 08 '17 at 02:56
  • What is not a compiler error? – Benjamin Lindley Apr 08 '17 at 02:56
  • 5
    @user7835540 maybe you should post an example of the code you are talking about in your comment "it's useful to take advantage of the "const" property of a reference; but this hides logic bugs since it is not a hard error". Reference reseating is syntactically impossible, therefore whatever you have in mind that "is not a hard error" is not reference reseating. – M.M Apr 08 '17 at 02:59
  • It would not complicate the language but miss the point of a reference. If you have `int a; int b; int& c = a; c = b;` the last assignment is not an error at all. It is actually the point of a reference to be able to change the value "pointed". Il you want mutable references, look at std::reference_wrapper. – Guillaume Gris Apr 08 '17 at 06:21

0 Answers0