Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is 0 distinguished from other integers when initializing nullptr_t?

As I understand, std::nullptr_t can be initialized from nullptr as well as from 0. But at the same time the third initialization below doesn't work, despite 5 has the same type as 0:

#include <memory>

int main()
{
    std::nullptr_t null1=0;
    std::nullptr_t null2=nullptr;
    std::nullptr_t null3=5; // error: cannot convert ‘int’ to ‘std::nullptr_t’ in initialization
}

How does this work? I.e. how does the standard library distinguish 0 from 5 at compilation time, if these literals aren't template arguments?

Can one create a custom class which would similarly distinguish arguments of its constructor at compilation time, not using std::nullptr_t for this?

like image 634
Ruslan Avatar asked Nov 12 '15 11:11

Ruslan


People also ask

How do you initialize a pointer to 0?

A pointer can also be initialized to null using any integer constant expression that evaluates to 0, for example char *a=0; . Such a pointer is a null pointer. It does not point to any object.

What is the difference between null and 0 in C++?

null means that there is no value found. and 0 means zero but zero is still a value which is zero.

What is special about a pointer that has the value Nullptr or 0?

A null pointer has a reserved value that is called a null pointer constant for indicating that the pointer does not point to any valid object or function. You can use null pointers in the following cases: Initialize pointers.

Does null 0 in C++?

In C++, Null is defined as 0.

What is the difference between null and nullptr in C++?

The nullptr has the 0 th index of the memory reference when compared to the null value the value specifies void data type is the null also nullptr is only defined using the pointer concept mainly the memory reference of the nullptr variable is 0 also the type is considered using the pointer type.

Does null pointer exist in both implicit and explicit conversations?

Therefore it exists in both implicit and explicit conversations and is mainly pointed out in the implicit conversions from the keyword nullptr into null pointer values is of include any pointer types and also it converts into any pointer to other member types.

What is the difference between null and zero?

Usually, however, “NULL” is just a synonym for zero and therein lies the problem: It is both a null pointer constant and arithmetic constant. This can raise a problem if you pass “NULL” as a parameter to a function. Let’s understand this problem with the following code snippet,

How to call a function with nullptr as the argument?

Generally, the keyword nullptr is denoted using the pointer literals in both variables and functions. It is also called a prvalue of type std::nullptr_t; using these function; we can call the nullptr and the null value as the arguments.


Video Answer


3 Answers

A nullptr_t can be only assigned the value nullptr or 0 which is implicitly converted.

According to N4296 (page.86):

4.10 Pointer conversions

A null pointer constant is an integer literal with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. [...] A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t.

You can not create a similar type within C++ yourself.

std::nullptr_t is implemented as a built-in type and its distinct properties are enforced by the compiler.


EDIT: Fixed paragraph on built-in types. Thanks Yakk!

like image 77
oo_miguel Avatar answered Oct 16 '22 09:10

oo_miguel


how does the standard library distinguish 0 from 5 at compilation time, if these literals aren't template arguments?

This has nothing to do with the standard library at all, nullptr_t is a built-in type known to the compiler, and obviously the compiler knows the difference between 5 and 0

Can one create a custom class which would similarly distinguish arguments of its constructor at compilation time, not using std::nullptr_t for this?

In general no.

You can write a type that can be initialized from 0 and not from 5 by making it take an argument of a pointer type, because 0 is a valid null pointer constant but 5 is not. But you couldn't write a type that can be constructed from 3 and not from 5, or anything else like that.

like image 11
Jonathan Wakely Avatar answered Oct 16 '22 11:10

Jonathan Wakely


N3337 [conv.ptr]/1: A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion. A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t.

0 is a null pointer constant of integral type, so it can be converted to a prvalue of type std::nullptr_t. 5 is not a null pointer constant, so it can't be.

like image 11
TartanLlama Avatar answered Oct 16 '22 09:10

TartanLlama