Perfectly emulate nullptr
You compiled it with C++0x compiler that failed for unknown reason. It compiles fine in C++03.
What header file needs to be included for using nullptr in g++?
GCC 4.4.1 does not support nullptr
.
Support for nullptr
was added in GCC 4.6.0:
http://gcc.gnu.org/gcc-4.6/changes.html
Improved experimental support for the
upcoming C++0x ISO C++ standard,
including support for nullptr (thanks
to Magnus Fromreide), noexcept,
unrestricted unions, range-based for
loops (thanks to Rodrigo Rivas Costa),
implicitly deleted functions and
implicit move constructors.
For earlier versions of GCC, if you want to experiment with nullptr
you can try the workaround in this SO question:
Can nullptr be emulated in GCC?
nullptr not declared when using -std=c++0x
Looking at the feature list of GCC it clearly shows :
Null pointer constant | N2431 | GCC 4.6
Which means that nullptr
was implemented in GCC 4.6. GCC 4.4.7 doesn't support it yet, no flag is going to fix that. You're going to have to update your compiler if you want to use it.
Why is nullptr undeclared in C?
nullptr
is C++ only; it is not needed in C because in C ((void*)0)
is convertible to any other pointer type without casts.
If you really really really like to type nullptr
in C, you can use
#define nullptr ((void*)0)
and it would then work mostly the same.
Notice that C has the NULL
macro from <stddef.h>
; it is readable too, but its expansion is implementation-defined, so it might be either ((void*)0)
or 0
(or something really strange); if it expands to 0
, you wouldn't get any diagnostics from
int a = NULL;
unique_ptr, nullptr and supporting gcc 4.5.x and 4.6.x
What errors did you hit?
#include <iostream>
#include <memory>
int main() {
using namespace std;
unique_ptr<int> up( new int( 30 ) );
if (up == 0)
cout << "nullptr!\n";
else cout << "bam!\n";
}
compiles fine with with g++ -std=c++0x -Wall nullptr.cpp -o nullptr
(gcc 4.6.2).
Also, go through N2431 paper by Stroustrup and Sutter on nullptr
where a similar usage (comparison with 0
) is explicitly listed in one of the examples.
C++ Impossible nullptr call mystery
Even If I try to dereference it on purpose, it DOES succeed.
No, the program has undefined behavior meaning it is still in error even if it doesn't say so explicitly and "seems to be working". This is due to the use of ->
for dereferencing in your program.
Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior. The program may just crash.
So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.
So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.
1For a more technically accurate definition of undefined behavior see this, where it is mentioned that: there are no restrictions on the behavior of the program.
implementation safe nullptr
You could probably create a "false" my_nullptr
of type my_nullptr_t
the following way:
const class my_nullptr_t
{
public:
/* Return 0 for any class pointer */
template<typename T>
operator T*() const
{
return 0;
}
/* Return 0 for any member pointer */
template<typename T, typename U>
operator T U::*() const
{
return 0;
}
/* Safe boolean conversion */
operator void*() const
{
return 0;
}
private:
/* Not allowed to get the address */
void operator&() const;
} my_nullptr = {};
This works with C++03 and C++11 and should always be safe, whichever C++11 features are implemented. That solution was actually already discussed in this topic that proposed a version of nullptr_t
based on the Official proposal.
Strong typing of nullptr?
I haven't read the actual spec on this one, but I'm pretty sure that the call you indicated would be ambiguous without a cast, since a null pointer can be converted to a pointer of any type. So the cast should be necessary.
And no, unfortunately, nullptr
is not a template. I really like that idea, though, so you could consider writing a function like this one:
template <typename PtrType> PtrType null() {
return static_cast<PtrType>(nullptr);
}
And then you could write
f(null<int*>());
Netbeans hating nullptr but still working fine
For Netbeans 7.4
In Project Properties:
Set "C Standard" to "C11" for the editor and "Additional Options" add "-std=c++0x" for the compiler
In my case, I had to close and reload the files and the error mark disappears.
Edit:
This is not a 100% working Solution, sorry.
Related Topics
Platform-Independent Guid Generation in C++
Why Is (Void) 0 a No Operation in C and C++
What's the Usual Way of Controlling Frame Rate
Pack Expansion for Alias Template
C++, How to Call a Constructor Directly, Without New
Is Opencv Matrix Data Guaranteed to Be Continuous
How Does an Extern "C" Declaration Work
Why We Need to Return Reference to Istream/Ostream While Overloading >> and << Operators
How to Add Wtl and Atl to Visual Studio C++ Express 2008
Performance Penalty for Working with Interfaces in C++
How to Find the Name of the Calling Function
How Does =Delete on Destructor Prevent Stack Allocation
Why Do We Use Std::Function in C++ Rather Than the Original C Function Pointer
Opencv Grouprectangles - Getting Grouped and Ungrouped Rectangles
What Is Use of the Ref-Qualifier 'Const &&'
In Lambda Functions Syntax, What Purpose Does a 'Capture List' Serve