In the hello world example of policy based design from wikipedia we have this very nice way of defining the implementation (policy-) specific member functions via using
directives:
template <typename OutputPolicy, typename LanguagePolicy>
class HelloWorld : private OutputPolicy, private LanguagePolicy {
public:
// Behavior method.
void Run() const {
// Two policy methods.
Print(Message());
}
private:
using LanguagePolicy::Message;
using OutputPolicy::Print;
};
Now assume that you want to allow a user to forget implementing the Message
method in the language policy. Hence, you define a fallback class
class DefaultLanguagePolicy {
protected:
std::string Message() const { return ""; }
}
But how do you enable this default if Message
is not implemented in the HelloWorld
Class and discard it otherwise?
template <typename OutputPolicy, typename LanguagePolicy, typename DefaultLanguagePolicy>
class HelloWorld : private OutputPolicy, private LanguagePolicy, private DefaultLanguagePolicy {
public:
// Behavior method.
void Run() const {
// Two policy methods.
Print(Message());
}
private:
// what do I need to write to get this kind of behaviour:
if (LanguagePolicy has Message implemented) {
using LanguagePolicy::Message;
} else {
using DefaultLanguagePolicy::Message;
}
using OutputPolicy::Print;
};
Thanks for any suggestions that ideally allow to keep this nice using
syntax.
Aucun commentaire:
Enregistrer un commentaire