You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
902 B
37 lines
902 B
6 years ago
|
/*
|
||
|
* Copyright (c) 2018-present, Facebook, Inc.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*/
|
||
|
#include <iostream>
|
||
|
|
||
|
namespace temporaries {
|
||
|
|
||
|
struct A {
|
||
|
int f;
|
||
|
};
|
||
|
|
||
|
std::unique_ptr<A> some_f();
|
||
|
|
||
|
void FN_call_some_f_deref_bad() {
|
||
|
const A& a_ref = *some_f(); // temporary unique_ptr returned by `some_f` is
|
||
|
// destroyed at the end of the statement
|
||
|
std::cout << a_ref.f;
|
||
|
}
|
||
|
|
||
|
void call_some_f_ok() {
|
||
|
auto local = some_f(); // ok, as ownership of a temporary unique_ptr is passed
|
||
|
// to `local`
|
||
|
const A& a_ref = *local;
|
||
|
std::cout << a_ref.f;
|
||
|
}
|
||
|
|
||
|
void call_some_f_copy_object_ok() {
|
||
|
auto a = *some_f().get(); // ok, as value is copied before temporary
|
||
|
// unique_prt is destroyed
|
||
|
std::cout << a.f;
|
||
|
}
|
||
|
|
||
|
} // namespace temporaries
|