Questions tagged [nullptr]

The C++11 keyword for a null pointer, it can be converted to any pointer type. Also available in C23 through stddef.h. Always use this tag either in combination with the C++ or the C tag.

In C++, nullptr is a null pointer constant of type std::nullptr_t which can be implicitly converted to any pointer type. An integer constant 0 can also be converted to any pointer type but can also be deduced as an integer in other contexts, leading to ambiguities. See What exactly is nullptr?

In C ("C23" ISO 9899:202x), nullptr is similarly a null pointer constant of type nullptr_t found in stddef.h. It can get converted to a void*, bool or a pointer type.

349 questions
16
votes
2 answers

Is nullptr_t a default constructible type?

I can't tell from the C++11 Standard if nullptr_t has a default constructor. In other words, is the following valid?: nullptr_t n; GCC and VC++ allow the above code, but clang does not. I can't find anything in the Standard specifying that it…
ThreeBit
  • 608
  • 6
  • 17
16
votes
2 answers

c++ is it required to handle nullptr in user-defined and class-specific delete operators.?

Is it required for user-defined and class-specific delete operators to ignore nullptr as that operators from standard library do? parallel discussion at google groups.
Volodymyr Boiko
  • 1,533
  • 15
  • 29
16
votes
4 answers

Why can't nullptr convert to int?

Summary: nullptr converts to bool, and bool converts to int, so why doesn't nullptr convert to int? This code is okay: void f(bool); f(nullptr); // fine, nullptr converts to bool And this is okay: bool b; int i(b); // fine, bool…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
15
votes
2 answers

Does upcasting a null pointer lead to undefined behavior

I'm wondering whether the following code leads to undefined behavior: #include #include struct IA { virtual ~IA() {} int a = 0; }; struct IB { virtual ~IB() {} int b = 0; }; struct C: IA, IB {}; int main() { C* pc =…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
14
votes
2 answers

What is the type of nullptr?

The Standard states, that nullptr is a pointer literal of type std::nullptr_t (2.14.7). And 18.2p9 defines nullptr_t by namespace std { typedef decltype(nullptr) nullptr_t; } By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr,…
MWid
  • 4,429
  • 3
  • 20
  • 20
13
votes
5 answers

The nullptr and pointer arithmetic

Considering the following code, is it safe to do pointer arithmetic on nullptr? I assume adding any offsets to a nullptr results in another nullptr, so far MSVC produce results as I expected, however I am a bit unsure about whether using nullptr…
user2188453
  • 1,105
  • 1
  • 12
  • 26
13
votes
3 answers

Compile error 'nullptr' undeclared identifier

I'm trying to compile a source with Visual Studio 2008 Express, but I'm getting this error: Error C2065: 'nullptr' undeclared identifier. My code: if (Data == nullptr) { show("Data is null"); return 0; } I read on Google that I should…
VIclean
  • 155
  • 1
  • 2
  • 7
13
votes
2 answers

Using Qt Creator C++ 11, nullptr is keyworded?

I'm using C++11 using Qt Creator. "warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]" "error: 'nullptr' was not declared in this scope" This is on code that works elsewhere, the relevant part being: ... = nullptr; What might be…
alan2here
  • 3,223
  • 6
  • 37
  • 62
12
votes
5 answers

Check if only one string variable is not nullptr in C++

I have three LPCWSTR string variables called A, B, C. I am assigning them from another function which can sometimes return nullptr if something goes wrong. like this: A = MyFunc(); B = MyFunc(); C = MyFunc(); Now, for some stuff with those…
Blueeyes789
  • 543
  • 6
  • 18
11
votes
1 answer

using nullptr instead of NULL when mixing C and C++

i have a very simple question... i am using the SDL API which was written in C. i am using C++. my compiler supports the keyword nullptr, and I've been reading up on it. it seems as if it is better to use rather than using the NULL macro. when I…
john
  • 147
  • 1
  • 7
11
votes
1 answer

Using nullptr in API function calls?

Using C++ with Visual Studio 2010. I'm in the process of converting my NULL's to nullptr's. With my code this is fine. However if I make a call to WINAPI such as: __checkReturn WINOLEAPI OleInitialize(IN LPVOID pvReserved); normally I would have…
User
  • 62,498
  • 72
  • 186
  • 247
11
votes
1 answer

Strong typing of nullptr?

I just read an article on the C++0x standard: http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-why-you-should-care/ It said nullptr was strongly typed, meaning that it can be distinguished from an integer…
Dov
  • 8,000
  • 8
  • 46
  • 75
11
votes
2 answers

How to use nullptr properly?

currently I am reading "A tour of C++" by Byarne Stroustrup. The thing that matters: on "pointers, arrays and references" he gave an example about using nullptr like this: int count_x(char* p, char x) // count the number of occurrences of x in…
WonFeiHong
  • 445
  • 4
  • 16
11
votes
1 answer

Why am I getting a "parameter set but not used" warning when using nullptr_t?

I have a custom class implementing operator== with nullptr. Here is my code dumbed down into a simple example: #include #include class C { private: void *v = nullptr; public: explicit C(void *ptr) : v(ptr) { } …
Venemo
  • 18,515
  • 13
  • 84
  • 125
11
votes
2 answers

How to define nullptr for supporting both C++03 and C++11?

Possible Duplicate: “Backporting” nullptr to C++-pre-C++0x programs How to define nullptr for supporting both C++03 and C++11? Does below code is compiled with both C++03 and C++11 compiles without change the meaning of nullptr in C++11…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
1 2
3
23 24