#include #include // This code will kill your mood, steal your cat and invade russia template auto apply(const F &f) -> decltype(f()) { return f(); } class Foo { int val; public: Foo() = default; Foo(const int &x) : val(x) {} Foo operator()() const { return Foo(2 * val); } operator int() const { return val; } }; int main() { std::vector v = {1, 2, 3, 4, 5}; std::vector b; std::transform(v.begin(), v.end(), std::back_inserter(b), apply); }