std::unique_ptr<int> ptr() {
std::unique_ptr<int> p(new int(3));
return p; // Why doesn't this require explicit move using std::move?
} // Why didn't the data pointed to by 'p' is not destroyed here though p is not moved?
int main() {
std::unique_ptr<int> a = ptr(); // Why doesn't this require std::move?
std::cout << *a; // Prints 3.
}
上記のコードでは、関数ptr()
は のコピーを返しますp
。範囲外にp
なると、データ「3」が削除されます。しかし、コードはアクセス違反なしでどのように機能するのでしょうか?