Can nullptr be emulated in gcc?

nuzz picture nuzz · Mar 10, 2010 · Viewed 19.3k times · Source

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

Answer

N 1.1 picture N 1.1 · Mar 10, 2010

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