Last active
August 29, 2015 14:27
-
-
Save erikzenker/6f117197bd69a97eb43d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> /* std::cout, std::endl */ | |
#include <type_traits> /* std::enable_if */ | |
// SFINAE test | |
template <typename T> | |
class has_extendedFoo { | |
typedef char one; | |
typedef long two; | |
template <typename C> static one test( decltype(&C::extendedFoo) ) ; | |
template <typename C> static two test(...); | |
public: | |
enum { value = sizeof(test<T>(0)) == sizeof(char) }; | |
}; | |
// POLICY1 | |
struct Policy1 { | |
void coreFoo(){ | |
std::cout << "core" << std::endl; | |
} | |
}; | |
// POLICY2 | |
struct Policy2 { | |
void coreFoo() { | |
std::cout << "core" << std::endl; | |
} | |
void extendedFoo() { | |
std::cout << "extended" << std::endl; | |
} | |
}; | |
// HOST | |
template <typename T_Policy> | |
struct Host : public T_Policy { | |
void foo() { | |
int result = foo_detail<T_Policy>(); | |
} | |
template<typename T> | |
typename std::enable_if<has_extendedFoo<T>::value, int>::type | |
foo_detail(){ | |
T::extendedFoo(); | |
return 0; | |
} | |
template<typename T> | |
typename std::enable_if<!has_extendedFoo<T>::value, int>::type | |
foo_detail(){ | |
T::coreFoo(); | |
return 0; | |
} | |
}; | |
int main(){ | |
Host<Policy1> core; | |
Host<Policy2> extended; | |
core.foo(); | |
extended.foo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usefull link from Stackoverflow