0

I tried the following with g++ 4.9.3:

int i = 0x1020;
char& ref1 = i;        // Error. Makes sense to me
char const& ref2 = i;  // No error. Does not make sense to me.

A problem I see:

char const* ptr1 = &i; // Error. Makes sense to me.
char const* ptr2 = &ref2;  // No Error. Circumvents the error in the previous line
                           // without any ugly looking casts.

Is the code in the third line above legal by the C++ Standard? References to the supporting section(s) of the standard will be appreciated.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 3
    Please correct me if I'm wrong, but isn't that just binding a temporary `char` to a const lvalue reference? Assigning an integer to a `char&&` [also works](http://coliru.stacked-crooked.com/a/ef93c741022bd07d) under both clang and gcc, so I'd assume this is a result of the rules for binding references to temporaries. – jaggedSpire Dec 04 '15 at 17:08
  • 5
    For the first part see [this](http://stackoverflow.com/questions/13780417/assigning-char-to-int-reference-and-const-int-reference-in-c). As to the second part see [this](http://stackoverflow.com/questions/13280437/is-it-legal-to-take-the-address-of-a-const-lvalue-reference) – NathanOliver Dec 04 '15 at 17:11
  • 1
    I think @jaggedSpire is right, [demo](http://coliru.stacked-crooked.com/a/8dd802388762e4d6). – Baum mit Augen Dec 04 '15 at 17:13
  • Thanks for your input guys. It makes sense now. – R Sahu Dec 04 '15 at 17:15

0 Answers0