Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can nullptr be emulated in gcc?

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

like image 943
nuzz Avatar asked Mar 10 '10 19:03

nuzz


2 Answers

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 
like image 85
N 1.1 Avatar answered Sep 24 '22 22:09

N 1.1


It looks like gcc supports nullptr as of 4.6.

like image 24
Roman A. Taycher Avatar answered Sep 25 '22 22:09

Roman A. Taycher