260.md
September 5, 2022 ยท View on GitHub
Example
#include <functional>
int main() {
{
std::function<int()> f{[]{return 42; }};
auto copy = f; // okay
auto value = f();
}
{
std::move_only_function<int()> f{[] {return 42; }};
auto copy = f; // error, call to deleted copy constructor
auto value = f(); // undefined behaviour, dandling reference
}
}
Puzzle
- Can you comment out and fill
???with appropirate function type (std::function or std::move_only_function) when applicable?
int main() {
{
auto f = [] { return 42; };
// TODO
// ??? f1 = f;
// ??? f2 = std::move(f);
}
{
auto value = 42;
auto f = [v = std::move(value)] { return v; };
// TODO
// ??? f1 = f;
// ??? f2 = std::move(f);
}
{
auto f = [u = std::make_unique<int>(42)] { return *u; };
// TODO
// ??? f1 = f;
// ??? f2 = std::move(f);
}
}