Created
November 14, 2016 09:38
-
-
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
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> | |
#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