Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ - Why is nullptr_t not assignable to int*? [duplicate]

cout << std::is_assignable<int*, std::nullptr_t>::value << endl;
cout << std::is_assignable<int*&, std::nullptr_t>::value << endl;

The output is: 0 1

I don't understand why the first check returns false

I can assign nullptr to a reference to pointer, but I cannot assign it to a raw pointer?

It's the inverse!

int* p = nullptr;
int*& pref = nullptr;

The second assignment, as expected, flags an error:

error: cannot bind non-const lvalue reference of type int*& to an rvalue of type int*

Can someone explain me what is going on?

like image 995
gedamial Avatar asked Oct 30 '22 04:10

gedamial


1 Answers

The documentation on cppreference covers this.

std::is_assignable<int, int> is false but std::is_assignable<int&, int> is true.

http://en.cppreference.com/w/cpp/types/is_assignable

like image 101
Richard Hodges Avatar answered Nov 15 '22 06:11

Richard Hodges