[pulse] another test for temporaries

Summary:
I rewrote the test so it doesn't need any C++ headers so that:
- it's easier to see what's going on
- it's easier to debug: the whole AST is now somewhat readable vs before
  the headers made it impossibly long

Reviewed By: ezgicicek

Differential Revision: D15674213

fbshipit-source-id: d98941983
master
Jules Villard 6 years ago committed by Facebook Github Bot
parent 1614f78f6d
commit c3d55817b1

@ -4,33 +4,59 @@
* This source code is licensed under the MIT license found in the * This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
#include <iostream>
namespace temporaries { namespace temporaries {
template <typename X>
struct UniquePtr {
X* x_;
~UniquePtr() {
if (x_) {
delete x_;
}
}
UniquePtr(X* y) { x_ = y; }
UniquePtr(UniquePtr<X>& p) = delete; // no copy constructor
UniquePtr(UniquePtr<X>&& p) {
x_ = p.get();
p.x_ = nullptr;
}
X* get() const { return x_; }
X& operator*() const { return *get(); }
X* operator->() const { return get(); }
};
struct A { struct A {
int f; int s_;
~A() {}
A() { A(42); }
A(int s) { s_ = s; }
A(A& a) { s_ = a.s_; }
}; };
std::unique_ptr<A> some_f(); UniquePtr<A> mk_UniquePtr_A() { return UniquePtr<A>(new A); }
void FN_call_some_f_deref_bad() { int FN_call_mk_UniquePtr_A_deref_bad() {
const A& a_ref = *some_f(); // temporary unique_ptr returned by `some_f` is A* a = mk_UniquePtr_A().get(); // temporary unique_ptr returned by
// destroyed at the end of the statement // `mk_UniquePtr_A` is destroyed at the end
std::cout << a_ref.f; // of the statement
return a->s_;
} }
void call_some_f_ok() { int call_mk_UniquePtr_A_ok() {
auto local = some_f(); // ok, as ownership of a temporary unique_ptr is passed const UniquePtr<A>& local =
// to `local` mk_UniquePtr_A(); // ok, as ownership of a temporary unique_ptr is passed
const A& a_ref = *local; // to `local`
std::cout << a_ref.f; return local->s_;
} }
void call_some_f_copy_object_ok() { int call_mk_UniquePtr_A_copy_object_ok() {
auto a = *some_f().get(); // ok, as value is copied before temporary A a = *mk_UniquePtr_A().get(); // ok, as value is copied before temporary
// unique_prt is destroyed // unique_prt is destroyed
std::cout << a.f; return a.s_;
} }
} // namespace temporaries } // namespace temporaries

Loading…
Cancel
Save