Skip to content

Instantly share code, notes, and snippets.

@sh19910711
Created March 25, 2013 22:03
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 sh19910711/5241225 to your computer and use it in GitHub Desktop.
Save sh19910711/5241225 to your computer and use it in GitHub Desktop.
メソッドのチェック
#include <iostream>
using namespace std;
struct TestA {
int needed_method() {
cout << "test" << endl;
return 0;
}
};
struct TestB {
int test() {
cout << "test" << endl;
return 0;
}
};
struct TestC: public TestA {
int test() {
cout << "test" << endl;
return 0;
}
};
struct TrueType { char a[1]; };
struct FalseType { char a[2]; };
template <class T> TrueType has_needed_method(typeof(&T::needed_method));
template <class T> FalseType has_needed_method(...);
template <class T> void check_method( const string& test_name ) {
cout << "# " << test_name << endl;
cout << ( sizeof(has_needed_method<T>(0)) == sizeof(TrueType) ? "true" : "false" ) << endl;
cout << endl;
}
int main() {
check_method<int>("int");
check_method<TestA>("TestA");
check_method<TestB>("TestB");
check_method<TestC>("TestC");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment