Skip to content

Instantly share code, notes, and snippets.

@kizzx2
Created April 20, 2011 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kizzx2/931835 to your computer and use it in GitHub Desktop.
Save kizzx2/931835 to your computer and use it in GitHub Desktop.
C++ static "has_function" with SFINAE
#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