Created
April 20, 2011 16:27
-
-
Save kizzx2/931835 to your computer and use it in GitHub Desktop.
C++ static "has_function" with SFINAE
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> | |
struct Foo | |
{ | |
void One(); | |
}; | |
struct Baz : Foo | |
{ | |
void Two(); | |
}; | |
struct Bar | |
{ | |
int One(int); | |
}; | |
template <class T> | |
struct HasOne | |
{ | |
typedef char Yes[1]; | |
typedef char No[2]; | |
template <typename U, U> | |
struct Check; | |
template <typename C> | |
static Yes & Test(Check<void (C::*)(), &C::One> *); | |
template <typename> | |
static No & Test(...); | |
static const bool value = sizeof(Test<T>(0)) == sizeof(Yes); | |
}; | |
int main() | |
{ | |
std::cout << HasOne<Foo>::value << std::endl; // 1 | |
std::cout << HasOne<Bar>::value << std::endl; // 0 | |
std::cout << HasOne<Baz>::value << std::endl; // 0 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment