これがなぜ失敗するのか理解できませんか?unique_ptrをcoutに出力する
int *p = new int(10);
std::unique_ptr<int> ptr(p);
// Below line gives compilation error.
std::cout << "Value of ptr " << ptr << std::endl;
// Below line works well.
std::cout << "Value pointed ptr " << *ptr << std::endl;
std::cout << "Value of ptr->get() " << ptr.get() << std::endl;
私はそれをこのように理解して:
は、新たに割り当てられたメモリのアドレスがunique_ptrを新しく割り当てられたメモリを指している、上記の描写で200
p new allocated memory
---------- ---------
200 10
---------- ---------
100 200
ptr
----------
200
----------
300
で、Pのアドレスが100であると言いますそれ自体、 'p'を避ける。だから、 'ptr'を印刷して200を与えるべきではないのですか?
Oho。意味をなさないC++コミュニティを停止していたものを購入して<<をオーバーロードする? –
@HemantBhargava:本当に必要ありません。 C++標準は既に大きすぎて(*)、ほとんど誰も使用しないようなものを追加すると、それが大きくなります。 *:いいえ、私は何を削除するかに関する良い提案はありません。 –
@MartinBonner間引きの提案については、 'std :: vector'が気になります。 –
Angew