Skip to content

Instantly share code, notes, and snippets.

@suma
Created June 21, 2012 16:27
Show Gist options
  • Save suma/2966842 to your computer and use it in GitHub Desktop.
Save suma/2966842 to your computer and use it in GitHub Desktop.
c++ is_derived implementation
is_derived r: 1
is_derived x: 1
is_derived y: 0
#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