Is it possible to mount nptptr in gcc?

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

Is there a way to "imitate" him? Something like:

 #define nullptr NULL 

(Obviously, this would not work well, just to show what I mean.)

+46
c ++ gcc c ++ 11 nullptr
Mar 10 '10 at 19:15
source share
5 answers

The official offer 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 TC::*() const // member pointer... { return 0; } private: void operator&() const; // whose address can't be taken } nullptr = {}; // and whose name is nullptr 
+60
Mar. 10 '10 at 19:27
source share

It seems that gcc supports nullptr as 4.6 .

+8
Nov 17 '10 at 5:10
source share

In addition, gcc (actually g ++) has had the __null extension for many years. This was taken into account as industry implementation experience when the nullptr offer came about.

The __null extension can detect special cases and warn of them, for example, accidentally passing NULL to the bool parameter when it should have been passed to the pointer parameter (changes made to the function forgot to adapt the call side).

Of course, this is not portable. The template solution above is portable.

+6
Jun 30 2018-11-11T00:
source share

It looks like gcc 4.6.1 (Ubuntu 11.11 oneiric), added nullptr.

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

 find . -name "*.[hc]pp" | xargs sed -i 's/NULL/nullptr/g' 
+4
Dec 01 '11 at 7:27
source share

Most likely you forgot -std = C ++ 0x. My MQL version of gcc version is 4.6.1 / 4.7.1, both support nulpt well.

As described in the "C ++ Standard Library, Tutorial and Reference, 2nd", nullptr is a keyword that can automatically convert to every pointer type, but not an integer type, this eliminates the NULL flaw, which is ambiguous for the next overload function : void f (int); void f (void *);

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

Testing 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

In fact, in VC2010, all of the above statements / expressions are legal. sizeof (nullptr) 4. typeid.name () result std :: nullptr_t, and throw nullptr can be detected by "const void *" and "void *" (and other types of pointers).

While gcc (4.7.1) looks tougher with respect to nullptr, throw nullptr cannot be captured by "void *", it can be detected by "..."

+2
Jun 28 '12 at 7:13
source share



All Articles