-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconversion_detection.h
36 lines (30 loc) · 994 Bytes
/
conversion_detection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// conversion_detection.h
// Code taken almost verbatim from Andrei Alexandrescu's article:
// http://www.cuj.com/experts/1810/alexandr.html
// I need this helper or else dumb g++ chokes.
template <class T, class U>
struct ConversionHelper {
typedef char Small;
struct Big { char dummy[2]; };
static Small Test(U);
static Big Test(...);
static T MakeT();
};
template <class T, class U>
struct Conversion {
typedef ConversionHelper<T,U> H;
enum { exists = sizeof(H::Test(H::MakeT())) == sizeof(H::Small) };
enum { exists2Way = exists && Conversion<U, T>::exists };
enum { sameType = false };
};
template <class T>
class Conversion<T, T> {
public:
enum { exists = 1, exists2Way = 1, sameType = 1 };
};
// I dislike the SUPERSUBCLASS macro, so I use this.
template <class Derived, class Base>
struct Inherits {
enum { value = (Conversion<const Derived*, const Base*>::exists &&
!Conversion<const Base*, const void*>::sameType) };
};