There's quite a bit of info on this topic. This is more of a design question, but I will give examples.
Let's say that I literally want to pass around a profile class, that dictates policies of a user.
struct ApplicationAllowedPolicy
{
public:
bool hasAccess() { return true; }
}
struct ApplicationProhibitedPolicy
{
public:
bool hasAccess() { return false; }
}
template<typename ApplicationPolicy>
class Profile : private ApplicationPolicy
{
bool hasAccess() { return ApplicationPolicy::access(); }
}
int main()
{
Profile<ApplicationAllowedPolicy> allowed;
Profile<ApplicationProhibitedPolicy> prohibited;
// do something with allowed & prohibited
}
The above is all fine and dandy, but let's assume there are a lot of policies that need to be read in. 5 Seems like a realistic real world number, although it could be more. Then, let's assume that this profile will be applied to hundreds of instances, with the 5 policies varying greatly. To boot, the policy behavior would only be known at run time (read from file, db, whatever). This quickly becomes unscalable, unless I'm totally missing something.
I thought of doing a non-type template class as a policy.
template<int N>
struct ApplicationPolicy
{
int policy = N;
};
Profile<ApplicationPolicy<1>> allowed;
Profile<ApplicationPolicy<0>> prohibited;
I think this would indeed work for my situation, but I'm wondering if this is missing the point of policy based design. I'm having problems seeing the pros of this over just have Profile be a normal struct, and just set it's data members to true/false as needed.
Thoughts?