Skip to content

Instantly share code, notes, and snippets.

@erikzenker
Last active August 29, 2015 14:27
Show Gist options
  • Save erikzenker/6f117197bd69a97eb43d to your computer and use it in GitHub Desktop.
Save erikzenker/6f117197bd69a97eb43d to your computer and use it in GitHub Desktop.
#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();
}
@erikzenker
Copy link
Author

More link of an member detector on wikibooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment