-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_has_something.cpp
73 lines (61 loc) · 2.09 KB
/
cpp_has_something.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#define DEFINE_has_member(member_name) \
template <typename T> \
class temp_has_member_##member_name \
{ \
typedef char yes_type; \
typedef long no_type; \
template <typename U> static yes_type test(decltype(&U::member_name)); \
template <typename U> static no_type test(...); \
public: \
static constexpr bool value = sizeof(test<T>(0)) == sizeof(yes_type); \
}
/// Shorthand for testing if "class_" has a member called "member_name"
///
/// @note "DEFINE_has_member(member_name)" must be used
/// before calling "has_member(class_, member_name)"
#define temp_has_member(class_, member_name) temp_has_member_##member_name<class_>::value
#include<vector>
#include <iostream> // cout, endl
// #include <iomanip> // std::boolalpha
typedef struct CubeSphereObject
{
double x;
double y;
double z;
double width;
double length;
double height;
double heading;
} CubeSphereObject;
struct B
{
bool heading;
};
struct C
{
bool headings;
};
using std::cout;
using std::endl;
DEFINE_has_member(heading);
template<class T>
class test
{
public:
test(){
// check the existence of "heading", T as vector<value_type>
cout << "container has_member(T, heading) " << temp_has_member(typename T:: value_type, heading) << endl;
// If T is just a template without cantianer, it will be easier:
cout << "has_member(T, heading) " << temp_has_member(T, heading) << endl;
cout << endl;
}
~test(){}
};
int main()
{
// cout << std::boolalpha; // display "true" or "false" for booleans
test<std::vector<CubeSphereObject> > a;
test<std::vector<B> > b;
test<std::vector<C> > c;
return 0;
}