Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <type_traits>
- using namespace std;
- class Policy1 {
- };
- class Policy2 {
- };
- /********** General settings *********/
- template<typename Policy>
- struct PolicyTraits {
- static const bool somePolicyProperty = false;
- static const bool anotherPolicyProperty = true;
- };
- /****** Overriding settings for Policy1 ****************/
- template<>
- struct PolicyTraits<Policy1> {
- static const bool somePolicyProperty = false;
- static const bool anotherPolicyProperty = false;
- };
- class SomeClass {
- public:
- // Some general template
- template<typename Policy, typename ValueType>
- void SomeFunction(Policy policy, ValueType value) {
- cout << "General template: " << value << endl;
- }
- // The specialization is not avaliable for Policy1
- template<typename Policy>
- void SomeFunction(Policy policy, int value, typename enable_if<PolicyTraits<Policy>::anotherPolicyProperty>::type* = 0) {
- cout << "Some function: integer " << value << endl;
- }
- };
- int main() {
- cout << boolalpha;
- cout << PolicyTraits<Policy1>::anotherPolicyProperty << endl; // false
- cout << PolicyTraits<Policy2>::anotherPolicyProperty << endl; // true
- SomeClass someClass;
- someClass.SomeFunction(Policy2(), "policy 2"); // correct: General template
- someClass.SomeFunction(Policy2(), 123); // correct: Specialization
- someClass.SomeFunction(Policy1(), "policy 1"); // correct: General template
- someClass.SomeFunction(Policy1(), 456); // correct: General template
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement