Skip to content

Instantly share code, notes, and snippets.

@facontidavide
Created November 14, 2016 09:38
Show Gist options
  • Save facontidavide/0c331825114a8b4d4d791eb3a2da151b to your computer and use it in GitHub Desktop.
Save facontidavide/0c331825114a8b4d4d791eb3a2da151b to your computer and use it in GitHub Desktop.
When using a template, check if the template argument has a specific method
#include <iostream>
#include <typeinfo>
struct Hello
{
int helloworld() { return 0; }
};
struct Generic {};
// SFINAE test
template <typename T>
class has_helloworld
{
typedef char one;
typedef int two;
template <typename C> static one test( decltype(&C::helloworld) ) ;
template <typename C> static two test(...);
public:
static bool value() { return sizeof(test<T>(0)) == sizeof(char); }
};
int main(int argc, char *argv[])
{
std::cout << has_helloworld<Hello>::value() << std::endl;
std::cout << has_helloworld<Generic>::value() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment