19

Will C++11 implementations define NULLas nullptr?

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
Martin Ba
  • 37,187
  • 33
  • 183
  • 337

4 Answers4

18

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.

Community
  • 1
  • 1
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • On the other hand, the standard doesn't say that is has to be `nullptr`, even though that change was proposed. – Bo Persson Aug 26 '11 at 06:54
  • @Bo: I am not much of a standardista nor is English my mother-tongue so I assumed that *implementation-defined* meant exactly that, ie the Standard does not impose anything. – Matthieu M. Aug 26 '11 at 07:02
  • 4
    My point is that it was proposed that the standard *should* require NULL to be nullptr, but that didn't happen. – Bo Persson Aug 26 '11 at 07:16
  • @Bo: Ah okay, I wish it had, it could probably have uncovered a number of bugs. I may be a type/compiler freak, but I must admit I never quite understand people who refuse such changes. – Matthieu M. Aug 26 '11 at 08:37
9

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.

TeaWolf
  • 714
  • 5
  • 10
  • 1
    I've seen code that uses NULL to initialize an element of an array of char (it should have used `'\0'`). Personally I wouldn't mind breaking such code, but backward compatibility is a harsh mistress. – Keith Thompson Aug 26 '11 at 06:57
  • "... Microsoft found ... they decided to keep NULL ..." - nice info. Do you have any reference link? – Martin Ba Aug 26 '11 at 08:24
  • 1
    @Martin: I have that information from one of the Channel 9 videos with Stephan T. Lavavej (the Microsoft guy responsible for their STL implementation) about the C++0x features. I don't remember the exact video though. – TeaWolf Aug 26 '11 at 08:43
1

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Its not "upcoming" standard anymore. It is already a *current* standard. – Nawaz Aug 26 '11 at 07:18
  • @Nawaz: **It is not, not officially**. – Alok Save Aug 26 '11 at 07:28
  • 1
    It has become the ISO Standard on 12th August 2011. [See this](http://herbsutter.com/2011/08/12/we-have-an-international-standard-c0x-is-unanimously-approved/) which says `The next revision of C++ that we’ve been calling “C++0x” is now an International Standard!”` – Nawaz Aug 26 '11 at 07:35
  • @Nawaz: I hate to break it to you, but All we have is the C++0x FDIS, which is identical in content to what will be the C++11 standard in a few weeks. The FDIS was approved. It is not a standard until it is published. Your link is One of those rare ocassions where Herb erred, or even lied. – Alok Save Aug 26 '11 at 07:54
  • 4
    @Als: Herb is right. Per the ISO/IEC Directives Part 1, 2.7.8, voting is what makes an IS out of an FDIS. Publication is covered in 2.8 – MSalters Aug 26 '11 at 08:14
  • @Als: Herb lied? How exactly do **you** know? Are you in the Standard Committee? Publication makes it a hard-book & soft-book. Approval makes it an ISO Standard. Now whatever features committee adds to the language will be included in the next Standard of C++, not `C++11`. – Nawaz Aug 26 '11 at 08:29
  • Code like `int i = NULL;` won't work anymore when NULL is defined as nullptr, which is allowed. Code using NULL will not "work just as before" on some compilers so your answer is wrong. – Sjoerd Aug 29 '11 at 14:48
-1

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).

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41