Originally posted as TotW #148 on May 3, 2018
by Titus Winters, (titus@google.com)
Into the blue again after the money’s gone Once in a lifetime, water flowing underground –David Byrne
=delete for Lifetimes
Imagine you have an API that requires a reference to some long-lived object, but doesn’t take ownership of it.
class Request ...
// The provided Context must live as long as the current Request. void SetContext(const Context& context);
You think to yourself, “Hey, what happens if someone passes a temporary? That’s going to be a bug. But this is modern C++, I can prevent that!” So you rig together a change in the API, adding a deleted overload.
class Request ...
// The provided Context must live as long as the current Request. void SetContext(const Context& context); (...)