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
10
votes
2 answers

"Backporting" nullptr to C++-pre-C++0x programs

More or less what the title suggests. While I'm not yet using C++0x I'd like to be prepared for when it happens, and I'd also like to reduce the amount of code I have to rewrite to use some of its facilities. That way I can get backwards and…
Luis Machuca
  • 1,047
  • 9
  • 16
10
votes
2 answers

Does nullptr_t break type punning or pointer conversions?

Consider this union: typedef union { void* vptr; nullptr_t nptr; } pun_intended; nullptr_t is supposedly compatible with void* 1). Ok so what if we initialize the void* to some non-zero value? pun_intended foo = { .vptr = (void*)42 };…
Lundin
  • 195,001
  • 40
  • 254
  • 396
10
votes
5 answers

Why is "std::is_pointer::value" equal to false?

I read about std::is_pointer in C++. Then I wrote the program and check whether T is a pointer type or not using std::is_pointer. #include int main() { std::cout<
msc
  • 33,420
  • 29
  • 119
  • 214
10
votes
1 answer

Why can I use nullptr without including STL?

The C++ nullptr is of the type std::nullptr_t. Why does a program like int main() { int* ptr = nullptr; } still work, although it doesn't include any STL library?
Robert Hönig
  • 645
  • 1
  • 8
  • 19
10
votes
5 answers

In function call, why doesn't nullptr match a pointer to a template object?

Here is an example of a code that works perfectly: #include #include template< class D, template< class D, class A > class C, class A = std::allocator< D > > void foo( C< D, A > *bar, C< D, A > *bas ) { std::cout << "Ok!" <<…
user1574855
  • 135
  • 1
  • 5
10
votes
2 answers

C++11- Use nullptr all the time?

I'm just a little bit confused. When should I use nullptr? I've read on some sites that it should always be used, but I can't set nullptr for a non-pointer for example: int myVar = nullptr; // Not a pointer ofcourse Should I always use NULL…
user2774429
  • 201
  • 2
  • 7
9
votes
1 answer

Assigning a nullptr to a member-pointer

Can anyone tell me why the internal representation of a nullptr assigned to a data member pointer of type Class::* is -1 for MSVC, clang and g++? For a 64-bit system (size_t)1 << 63 would be best, because if you use a nullptr member pointer that way…
Bonita Montero
  • 2,817
  • 9
  • 22
9
votes
1 answer

Can't return nullptr for unique_ptr return type

I am writing a wrapper for SDL_Texture* raw pointer which returns a unique_ptr. using TexturePtr = std::unique_ptr; TexturePtr loadTexture(SDL_Renderer* renderer, const std::string &path) { ImagePtr…
9
votes
5 answers

c++: why can't 'this' be a nullptr?

In my early days with C++, I seem to recall you could call a member function with a NULL pointer, and check for that in the member function: class Thing {public: void x();} void Thing::x() { if (this == NULL) return; //nothing to do ...do…
Scott M
  • 684
  • 7
  • 14
9
votes
3 answers

Is nullptr in C++ the same as null in C#?

Is nullptr in C++ the same as null in C#? Seems like no one asked this question on Google or Stackflow.
whiteSkar
  • 1,614
  • 2
  • 17
  • 30
8
votes
1 answer

What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?

In ISO/IEC 14882:2017 (C++17), Section 5.13.7 "Pointer literals" is stated: 5.13.7 Pointer literals [lex.nullptr] pointer-literal: nullptr 1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [Note: std::nullptr_t…
8
votes
3 answers

Why is nullptr a part of the core language, but nullptr_t is a part of STL?

As far as I'm aware nullptr is a part of the core language. Quoting C++11: (18.2/9) nullptr_t is defined as follows: namespace std { typedef decltype(nullptr) nullptr_t; } and is defined in the header .
embedc
  • 1,485
  • 1
  • 6
  • 20
8
votes
1 answer

What C++0x Headers are supposed to define nullptr?

Now that C++0x is almost here, I've been experimenting with it, and in particular using nullptr. I haven't been able to figure out what standard header files one is supposed to include if one needs to use it. Any help is appreciated.
swestrup
  • 4,079
  • 3
  • 22
  • 33
8
votes
4 answers

Call of overloaded method with nullptr is ambiguous

I have some overloaed methods which take some different pointer types. Now I want to call one specific method with nullptr as a parameter. I know that I could cast the nullptr to the specific type of pointer, the method I want it to call…
Melvin S.
  • 83
  • 5
8
votes
2 answers

Assigned `nullptr` to `bool` type. Which compiler is correct?

I have a following snippet of code that assigned nullptr to bool type. #include int main() { bool b = nullptr; std::cout << b; } In clang 3.8.0 working fine. it's give an output 0. Clang Demo But g++ 5.4.0 give an…
msc
  • 33,420
  • 29
  • 119
  • 214