-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestPointerTraits.cpp
56 lines (45 loc) · 1.38 KB
/
testPointerTraits.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
// clang++ -std=c++11 testPointerTraits.cpp
#include <memory>
#include <type_traits>
#include <vector>
template <class T>
inline T* to_raw_pointer(T* p)
{
return p;
}
template <class Pointer>
inline typename std::pointer_traits<Pointer>::element_type*
to_raw_pointer(Pointer& p)
{
return to_raw_pointer(p.operator->());
}
template<typename T>
class SmartPoint {
public:
SmartPoint()
: m_ptr(nullptr)
{
}
SmartPoint(T* ptr)
: m_ptr(ptr)
{
}
static T pointer_to(SmartPoint<T> p)
{
return p.m_ptr;
}
private:
T* m_ptr;
};
int main (int argc, char const *argv[])
{
static_assert(std::is_same<std::pointer_traits<int*>::element_type, int>::value, "");
static_assert(std::is_same<std::pointer_traits<std::unique_ptr<int>>::element_type, int>::value, "");
static_assert(std::is_same<std::pointer_traits<std::unique_ptr<int>>::element_type*, int*>::value, "");
static_assert(std::is_same<std::pointer_traits<SmartPoint<int>>::element_type, int>::value, "");
static_assert(std::is_same<std::pointer_traits<SmartPoint<int>>::element_type*, int*>::value, "");
static_assert(std::is_same<std::pointer_traits<std::vector<int>>::element_type, int>::value, "");
std::unique_ptr<int> unique_ptr_to_int = std::unique_ptr<int>(new int(9));
int* int_pointer = to_raw_pointer(unique_ptr_to_int);
return 0;
}