Consider using overload resolution to resolve the proper logging-method.
- That allows you to template that member, and maybe use perfect forwarding.
Don't make useless costly copies. You can take arguments by
const&
.Avoid
std::endl
, as constant flushing also flushes any chance of good performance down the drain.struct Chatty { template<class T> static void log(T&& t) { std::cout << std::forward<T>(t) << '\n'; } }; struct Silent { template<class T> static void log(T&&) {} };
Your
WidgetMaker
is currently restricted toLoggingpolicies
andCreationPolicies
which are templates. That's a bad idea. Better accept a type-parameter like everyone else.doAll
assumes the logging-method is static. Why then do you assign it to astd::function
? (Yes, that function should have a signature avoiding copies, but that's a separate problem.)The easiest way to repair that is using a lambda. Or you can get fancy with SFINAE and maybe also allow the policy-class to only implement those parts it wants to override.
struct Chatty {
template<class T>
static void log(T&& t) { std::cout << std::forward<T>(t) << '\n'; }
};
struct Silent {
template<class T>
static void log(T&&) {}
};
Your
WidgetMaker
is currently restricted to policies which are templates. That's a bad idea. Better accept a type-parameter like everyone else.doAll
assumes the logging-method is static. Why then do you assign it to astd::function
? (Yes, that function should have a signature avoiding copies, but that's a separate problem.)The easiest way to repair that is using a lambda. Or you can get fancy with SFINAE and maybe also allow the policy-class to only implement those parts it wants to override.