2017-07-18 7 views
4
cout << std::is_assignable<int*, std::nullptr_t>::value << endl; 
cout << std::is_assignable<int*&, std::nullptr_t>::value << endl; 

出力されます生のポインタに割り当てることはできませんか?C++ - nullptr_tがint *に代入されないのはなぜですか? <strong>は0 1</strong></p> <p>私は最初のチェックは<strong>が偽</strong></p> <p>私は</em>をポインタへ<em>参照に<strong>nullptr</strong>を割り当てることができ返す理由を理解していないが、私:

逆です!

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

第二の割り当ては、予想通り、フラグエラー:

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

誰かが何が起こっているかを私に説明できますか?

+2

あなたがそれに割り当て、nullptrとの参照を初期化しないしようとしているからです。 '='あなたを混乱させてはいけません。 – VTT

+0

ここでは 'int *'の代入はありませんが、 'int *&'は誤っているようです。 – Fabien

+1

もちろん、 'int *'に代入することはできません。それ以外の場合、実際には 'int a = 1; &a = nullptr; ' –

答えて

2

cppreferenceのドキュメントではこれを扱っています。

std::is_assignable<int, int>はfalseですが、std::is_assignable<int&, int>がtrueです。

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

+1

はい、GSerg [記載されている重複](https:/ /)は、[この質問](https://stackoverflow.com/questions/19920213/why-is-stdis-assignable-counter-intuitive) //stackoverflow.com/questions/45177674/c-why-is-nullptr-t-not-assignable-to-int#comment77323990_45177674) –

関連する問題

 関連する問題