Contents
Can smart pointers be copied?
C++ Standard Library smart pointers Can be moved to a new owner, but not copied or shared. Replaces auto_ptr , which is deprecated.
Does null == nullptr?
The NULL constant gets promoted to a pointer type, and as a pointer it is a null pointer, which then compares equal to nullptr .
Do I need to initialize shared_ptr to nullptr?
Yes, it is correct to initialize shared_ptr with nullptr . It is also correct to assign nullptr to shared_ptr . Or should I return shared_ptr default constructed instead? return shared_ptr(nullptr);
Can Unique_ptr be null?
Nullability – a scoped_ptr or unique_ptr can be null, a value object can never be. Polymorphism – a value object is always exactly its static type, but you can substitute in different derived types for a unique_ptr. The previously-held object is automatically destroyed when you do this.
Should I use null or nullptr?
nullptr is a new keyword introduced in C++11. nullptr is meant as a replacement to NULL . nullptr provides a typesafe pointer value representing an empty (null) pointer. The general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past.
Is 0 same as nullptr?
Nullptr vs NULL NULL is 0 (zero) i.e. integer constant zero with C-style typecast to void* , while nullptr is prvalue of type nullptr_t , which is an integer literal that evaluates to zero. For those of you who believe that NULL is the same i.e. (void*)0 in C and C++.
How do you pass a unique pointer to a function?
Move the smart pointer into the function argument You could move the value, but that implies passing ownership of the object and control of its lifetime to the function. If the lifetime of the object is guaranteed to exist over the lifetime of the call to MyFunc, just pass a raw pointer via ptr. get() .
Can a nullptr be assigned to a pointer type?
In C++11 you would be able to overload on nullptr_t so that ptr p (42); would be a compile-time error rather than a run-time assert. nullptr can’t be assigned to an integral type such as an int but only a pointer type; either a built-in pointer type such as int *ptr or a smart pointer such as std::shared_ptr
What happens if a pointer is not null but points to a valid object?
If the pointer is not null but does not point to a valid object, then using the pointer causes undefined behaviour. To avoid this sort of error, the onus is on you to be careful with the lifetime of objects being pointed to; and the smart pointer classes help with this task.
How is nullptr used in return type resolver?
nullptr is a subtle example of Return Type Resolver idiom to automatically deduce a null pointer of the correct type depending upon the type of the instance it is assigning to. As you can above, when nullptr is being assigned to an integer pointer, a int type instantiation of the templatized conversion function is created.
Is there a way to convert an int to a null pointer?
Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL. So nullptr is a value of a distinct type std::nullptr_t, not int. It implicitly converts to the null pointer value of any pointer type.