Created
June 21, 2012 16:27
-
-
Save suma/2966842 to your computer and use it in GitHub Desktop.
c++ is_derived implementation
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
is_derived r: 1 | |
is_derived x: 1 | |
is_derived y: 0 |
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 <string> | |
#include <exception> | |
#include <stdexcept> | |
using namespace std; | |
struct yes_size { char c[256]; }; | |
yes_size dispatch_exception(exception const*); | |
struct no_size {}; | |
no_size dispatch_exception(void const*); | |
template <class T> | |
struct is_derived { | |
enum e { value = sizeof(dispatch_exception(static_cast<T*>(NULL))) == sizeof(yes_size) }; | |
}; | |
class X : public runtime_error {}; | |
class Y {}; | |
int main() | |
{ | |
cout << "is_derived r: " << is_derived<runtime_error>::value << endl; | |
cout << "is_derived x: " << is_derived<X>::value << endl; | |
cout << "is_derived y: " << is_derived<Y>::value << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment