typeTrait.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <type_traits>
6
7namespace by {
8
13 template <typename T> struct typeTrait {
14 typedef T Org;
15 typedef T& Ref;
16 typedef T* Ptr;
17
18 static T ret() { return T{}; } // return default value
19
20 static bool isNul(T) { return false; }
21
22 static constexpr nbool is_ptr = false;
23 static constexpr nbool is_ref = false;
24 static constexpr nbool is_like_ptr = is_ptr;
25 };
26
27 template <typename T> struct typeTrait<T&> {
28 typedef T Org;
29 typedef T& Ref;
30 typedef T* Ptr;
31
32 static nbool isNul(const T& it) { return true; }
33
34 static T& ret() {
35 static T* dummy = nullptr;
36 return *dummy;
37 }
38
39 static constexpr nbool is_ptr = false;
40 static constexpr nbool is_ref = true;
41 static constexpr nbool is_like_ptr = is_ptr;
42 };
43
44 template <> struct typeTrait<void> {
45 static void ret() {}
46
47 static bool isNul(void) { return false; }
48
49 static constexpr nbool is_ptr = false;
50 static constexpr nbool is_ref = false;
51 static constexpr nbool is_like_ptr = is_ptr;
52 };
53
54 template <typename T> struct typeTrait<T*> {
55 typedef T Org;
56 typedef T& Ref;
57 typedef T* Ptr;
58
59 static nbool isNul(const T* it) { return it == nullptr; }
60
61 static T* ret() { return nullptr; }
62
63 static constexpr nbool is_ptr = true;
64 static constexpr nbool is_ref = false;
65 static constexpr nbool is_like_ptr = is_ptr;
66 };
67
68 template <> struct typeTrait<void*> {
69 typedef void Org;
70 typedef void* Ptr;
71
72 static nbool isNul(const void* it) { return it == nullptr; }
73
74 static void* ret() { return nullptr; }
75
76 static constexpr nbool is_ptr = true;
77 static constexpr nbool is_ref = false;
78 static constexpr nbool is_like_ptr = is_ptr;
79 };
80
81 template <typename T> auto nul(T&& it) -> decltype(typeTrait<std::remove_reference_t<std::decay_t<T>>>::isNul(it)) {
82 return typeTrait<std::remove_reference_t<std::decay_t<T>>>::isNul(std::forward<T>(it));
83 }
84} // namespace by
Type trait utilities for template metaprogramming.
Definition: typeTrait.hpp:13