Martin Ba
Martin Ba

Reputation: 38961

Is NULL defined as nullptr in C++11?

Will C++11 implementations define NULLas nullptr?

Would this be prescribed by the new C++ standard?

Upvotes: 19

Views: 11869

Answers (4)

ivan.ukr
ivan.ukr

Reputation: 3581

NULL comes from C, so its definition must be compatible with both C and C++. So it can't be nullptr, because that is C++ keyword, not C, unless it is defined differently for C and C++ (we can use #ifdef __cplusplus to distinguish between them). So NULL is usually defined as smth compiler specific, like __nullptr in gcc, or just ((void*)0).

Upvotes: -1

TeaWolf
TeaWolf

Reputation: 714

No, NULL is still the same as before. Too many people used the NULL macro in surprising ways, redefining it to nullptr would have broken a lot of code.

To elaborate: people have used NULL for example for many kinds of handle typedefs. If the real type behind such a typedef is not a pointer, defining NULL as nullptr would be a problem. Also, it seems some people have indeed used NULL to initialize numeric types.

At least that is what Microsoft found when they added the nullptr to MSVC10, and why they decided to keep NULL as it always was. Other compilers might choose a different path, but I don't think they would.

Upvotes: 9

Matthieu M.
Matthieu M.

Reputation: 300409

From the horse's mouth

C.3.2.4 Macro NULL [diff.null]

1/ The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>, <cstring>, <ctime>, or <cwchar>, is an implementation-defined C++ null pointer constant in this International Standard (18.2).

It is up to each implementation to provide its own definition, gcc if I recall correctly defines it to __nullptr for which it has special checks (verifies that it is not used in arithmetic contexts for example).

So it is possible to define it as nullptr, you will have to check your compiler/Standard Library documentation to see what has been done.

Upvotes: 18

Alok Save
Alok Save

Reputation: 206646

FDIS of the upcoming standard C++11, integral expression is still a null pointer constant. NULL macro is still implementation defined but must be a null pointer constant. So in practice it means it is good as 0 or can be nullptr.

Your code that used either 0 or NULL will work just as before.

Read the details here.

Upvotes: 0

Related Questions