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

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

10 Comments

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: Then you aren't using C++0x. :)
Well, I'm using a part of C++0x ;)
just keep a tab on compiler implementation status of c++0x.
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?
|
10

It looks like gcc supports nullptr as of 4.6.

Comments

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.

1 Comment

__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.
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'

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.