Why do some c++ compilers let you take the address of a literal?

后端 未结 5 468
北荒
北荒 2021-01-03 08:08

A C++ compiler that I will not name lets you take the address of a literal, int *p = &42;

Clearly 42 is an r-value and most compilers refuse to do so.

W

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 08:59

    It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42

    Then why, in some compilers, we can't take the address of a literal directly ?

    int* ptr = &10;
    

    The reference:

    int& ref = 10;
    

    is almost the same thing as a pointer, though...

提交回复
热议问题