48

I saw that nullptr was implemented in Visual Studio 2010. I like the concept and want to start using it as soon as possible; however GCC does not support it yet. My code needs to run on both (but doesn't have to compile with other compilers).

Is there a way to "emulate" it? Something like:

#define nullptr NULL

(That obviously wouldn't work well at all, it's just to show what I mean.)

Xeo
  • 129,499
  • 52
  • 291
  • 397
nuzz
  • 481
  • 1
  • 4
  • 3

5 Answers5

63

The Official proposal has a workaround -

const                        // this is a const object...
class {
public:
  template<class T>          // convertible to any type
    operator T*() const      // of null non-member
    { return 0; }            // pointer...
  template<class C, class T> // or any type of null
    operator T C::*() const  // member pointer...
    { return 0; }
private:
  void operator&() const;    // whose address can't be taken
} nullptr = {};              // and whose name is nullptr
N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • 3
    Thanks. do you also know if there's a way to detect if `nullptr` is implemented, without relying on the version of __cplusplus (because technically I'm using C++0x, and nullptr isn't there) – nuzz Mar 10 '10 at 19:33
  • 10
    @nuzz: Then you aren't using C++0x. :) – Bill Mar 10 '10 at 19:34
  • 4
    Well, I'm using a part of C++0x ;) – nuzz Mar 10 '10 at 20:01
  • 1
    just keep a tab on compiler implementation status of c++0x. – N 1.1 Mar 10 '10 at 20:05
  • 1
    I think nuzz's question is, can this be detected, so the code would use the built-in nullptr instead of the emulated one if implemented? – UncleBens Mar 10 '10 at 20:31
  • if nullptr has been implemented, `char *str = nullptr` wont throw any error. – N 1.1 Mar 10 '10 at 20:44
  • To work with different compilers/versions, I would do something like what Boost does. Search the Boost headers for stuff like BOOST_WORKAROUND(__GNUC__, < 3). Long story short, differentiate between compilers by using the preprocessor. – Brent Bradburn Mar 10 '10 at 20:48
  • @Uncle: especially because if it's implemented that workaround code won't work because `nullptr` is a keyword, I believe – Andreas Bonini Mar 11 '10 at 14:00
  • @Andreas: Exactly. You could use the preprocessor, but I'd imagine C++0x support is in a state of great flux, so you'd have to constantly keep an eye on what is and what isn't implemented in different compiler versions. – UncleBens Mar 11 '10 at 14:12
  • 1
    The workaround works ok for =, but doesn't work for == or !=. This is noted in the document at the link. – codeDr Sep 16 '11 at 15:18
10

It looks like gcc supports nullptr as of 4.6.

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
6

Also, gcc (actually g++) has had an extension __null for years. This was counted as industry implementation experience when the nullptr proposal came out.

The __null extension can detect special cases and warn about them such as accidentally passing NULL to a bool parameter, when it was intended to be passed to a pointer parameter (changes made to a function, forgot to adapt the call side).

Of course this isn't portable. The template solution above is portable.

emsr
  • 15,539
  • 6
  • 49
  • 62
  • __null is just what NULL is defined to for gcc, which it uses to issue warnings. It is not different from using NULL, and does not have the same semantics as nullptr. – rdb Dec 03 '16 at 20:53
5

It looks by gcc 4.6.1 (Ubuntu 11.11 oneiric), nullptr has been added.

A quick, recursive sed find-and-replace on my hpp/cpp files worked fine for me:

find . -name "*.[hc]pp" | xargs sed -i 's/NULL/nullptr/g'
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
1

It's most likely you forgot -std=c++0x . My Mingw version of gcc is 4.6.1/4.7.1, both support nullptr well.

According to description in "The c++ standard library, a tutorial and reference, 2nd", nullptr is a keyword, can automatically convert to each pointer type but not integer type, this overcome the drawback of NULL, which is ambiguous to the following overload function: void f(int ); void f(void *);

f(NULL); // Ambiguous f(nullptr); // OK

Test this feature in VC2010 shows that the MSDN document conflicts with the actual compiler, the document said:

The nullptr keyword is not a type and is not supported for use with:

sizeof

typeid

throw nullptr

Actually in VC2010, all of the above operator/expression is legal. sizeof(nullptr) result 4. typeid.name() result std::nullptr_t, and throw nullptr can be caught by "const void *" and "void *"(and other pointer types).

While gcc(4.7.1) looks more rigid about nullptr, throw nullptr cannot be caught by "void *", can be caught by '...'

zhaorufei
  • 2,045
  • 19
  • 18
  • 4
    No, it's most likely the question is already 2 years old, from a time where gcc just didn't support `nullptr`. But the answers saying that 4.6 supports it are actually 1.5 years old, too, already. So I guess your "answer" is completely obsolete. – Christian Rau Jun 28 '12 at 07:31