The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
#include <functional>
int cube(int i) {
return i * i * i;
}
void callMO(std::move_only_function<int(int)> f) {
if (f) {
std::cout << f(7) << '\n';
}
}
int main()
{
std::move_only_function<int(int)> mofunc;
mofunc = cube; // refer to ordinary function
callMO(std::move(mofunc));
callMO(cube);
mofunc = std::negate<int>{}; // refer to function object
callMO(std::move(mofunc));
callMO(std::negate<int>{});
auto square = [] (int i) { return i * i; };
mofunc = square; // refer to lambda
callMO(std::move(mofunc));
callMO(square);
}