6

In the book "C++ Primer, 5th ed", section 2.4.4, the entire section explains about "constexpr". Then an exercise as below is given in the book:

Exercise 2.32: Is the following code legal or not? If not, how might you make it legal?

int null = 0, *p = null;

I understand that a quick way to fix it is to just change it to *p = nullptr, or NULL, or use reinterpret_cast. But I think the book's intention is to use something related to constexpr. So my question is, how would the above exercise be solved properly? (I think the book's intention is to init the value of p to 0, not the address of null.)

I made below trials but both failed at compilation:

trial 1, adding constexpr:

constexpr int null = 0; 
int *p = null;

trial 2, adding const;

const int null = 0;
int *p = null;

(I made this trial based on the wordings in book chapter 4.11.2, Other implicit conversions: "A constant integral value of 0 and the literal nullptr can be converted to any pointer type;")

Thank you in advance. (Reason being asked as a new question: this is asked as a new question in hope of finding a solution. there is another closely related question but no proposed solution was given Is this constexpr integer not a null pointer constant?)

0

2 Answers 2

5

The actual wording from the standard (4.10 [conv.ptr]) is:

1 - A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type [...]

The problem with null is that although it is an integral constant expression with value zero, it is an lvalue (that is, it is the name of object) and so not a prvalue.

Some ways to obtain an integer constant prvalue:

constexpr int null = 0; 
int *p = +null;

By prefixing null with the unary arithmetic operator + we get a prvalue.

const int null = 0;
int *p = static_cast<int>(null);

static_cast to a non-reference type yields a prvalue.

Sign up to request clarification or add additional context in comments.

3 Comments

However, see DR #903 [ open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#903 ]. The proposed resolution would make these workarounds ill-formed as well.
Both solution indeed worked. Although a bit weird from the book's structure as an exercise. (The lvalue/rvalue part is not introduced yet in chapter 2. )
Both solution yields an error for me. cannot initialize a variable of type 'int *' with an rvalue of type 'int'.
0

Is the following code legal or not? If not, how might you make it legal?

No. Its not legal. Since null is int type & is needed to initialize p.

int null = 0, 
int *p = &null;   

Otherwise your compiler should raise a warning:

warning: invalid conversion from 'int' to 'int*'

If you want to initialize p to address 0 the you can do as

int null = 0, *p = 0;  

Now it is perfectly legal because 0 can be used in place of NULL.

c++-faq:

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

9 Comments

thanks for answering. But I think the book's intention is to initialize p 's value to 0. Your code will pass compilation but gives an non-zero address.
@Jefffrey; How? You mean null is equivalent to NULL here or somthing else?
hi, from my side I get below prompt about errors: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
p is being initialized, just with an invalid type object. If int null = 0, *p = null; was int null = 0, *p = 0;, then it would be perfectly legal.
@Jefffrey; But this is not the case here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.