Created
March 25, 2013 22:03
-
-
Save sh19910711/5241225 to your computer and use it in GitHub Desktop.
メソッドのチェック
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> | |
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