Introduction
Every software developer in C++ is faced with the generic programming. Some of them have started to use the Policy design pattern. I have spent much time on research of that pattern and found an interesting form of it with the feature of C++11 - Variadic template technique.
Background
I have spent much time on research of Policy design pattern and implemented it once in one commercial project. However, some time ago, I decided to improve my project by including C++11 features and found a very nice form for Policy pattern.
Using the Code
The new form of Policy pattern can have the next form:
template<typename... Policies>
class PolicyAndVaradic: public Policies{};
This form is different from the old form:
template<typename PolicyOne, typename PolicyTwo>
class PolicyOldForm: public PolicyOne, PolicyTwo{};
It leads to the interesting idea - it is possible to create a class which amount of policies is resolved in the time of compiling.
So, you can ask - how it can be used? I wrote a simple code:
template<typename... Policies>
class PolicyAndVariadic: public Policies...{};
class PolicyOne
{
public:
void executePolicyOne()
{
std::cout << "execute PolicyOne" << std::endl;
}
};
class PolicyTwo
{
public:
void executePolicyTwo()
{
std::cout << "execute PolicyTwo" << std::endl;
}
};
typedef PolicyAndVariadic<PolicyOne, PolicyTwo> PolicyOneAndPolicyTwo;
PolicyOneAndPolicyTwo linstance;
linstance.executePolicyOne();
linstance.executePolicyTwo();
As you see, in the code the template class PolicyOneAndPolicyTwo
includes implementation of classes PolicyOne
and PolicyTwo
via the template PolicyAndVariadic
. In old style, the amount of Policies was limited by old standards of C++. The C++11 allows to get more nice view policy template class.
You can ask - it works perfectly with default constructors, but what to do if we want to include policies, which constructors have to arguments? It leads to the next nice view form:
template<typename... Policies>
class PolicyAndVaradic: public Policies...
{
public:
template<typename... Args>
PolicyAndVaradic(const Args... Arg)
: Policies(Arg)...{}
};
class PolicyOne
{
std::string mText;
public:
PolicyOne(const char* aText):mText(aText){}
void executePolicyOne()
{
std::cout << mText << std::endl;
}
};
class PolicyTwo
{
std::string mText;
public:
PolicyTwo(const char* aText):mText(aText){}
void executePolicyTwo()
{
std::cout << mText << std::endl;
}
};
PolicyOneAndPolicyTwo linstance("PolicyOne", "PolicyTwo");
linstance.executePolicyOne();
linstance.executePolicyTwo();
So, I think that I do not need to explain how it works.
Of course, you can say the every serious library on policy pattern needs more complex actions with the policies and this form cannot be suitable. I agree with it. However, if you want to create a simple template solution with static polymorphism, then the policy pattern with variadic template can be a suitable solution.
Points of Interest
I think that it could be interesting to include a Lambda expression pattern into the policy pattern.
History
- 8th April, 2015: Initial version