Supplier is a function taking no arguments and returning some type: you can represent that with std::function:
#include <iostream>
#include <functional>
#include <memory>
// the class Employee with a "print" operator
class Employee
{
friend std::ostream& operator<<(std::ostream& os, const Employee& e);
};
std::ostream& operator<<(std::ostream& os, const Employee& e)
{
os << "A EMPLOYEE";
return os;
}
// maker take the supplier as argument through std::function
Employee maker(std::function<Employee(void)> fx)
{
return fx();
}
// usage
int main()
{
std::cout << maker(
[]() { return Employee(); }
// I use a lambda here, I could have used function, functor, method...
);
return 0;
}
I didn't use pointers here nor the operator new to allocate Employee: if you want to use it, you should consider managed pointers like std::unique_ptr:
std::unique_ptr<Employee> maker(std::function<std::unique_ptr<Employee>(void)> fx)
{
return fx();
}
// ...
maker(
[]()
{
return std::make_unique<Employee>();
}
);
Note: the call to operator<< should then be modified because maker will return a pointer instead of an object.