|
3 | 3 |
|
4 | 4 | A simple, header-only cpp library implementing smart pointer type traits. |
5 | 5 | You can easily compile your code diffrently depending on which pointer-type is used. |
6 | | -You can find inheritance and you won't have trouble with const, volatile keywords. |
| 6 | +You can detect inheritance too and you won't have trouble with const, volatile keywords. |
7 | 7 |
|
8 | 8 | Type traits are generally used for static assertions and static polymorphism. (C++ 17 if constexpr) |
9 | | -However, the standard library's type traits do not support smart pointers, and do not find inheritance. |
| 9 | +However, the standard library's type traits do not support smart pointers, and do not detect inheritance. |
10 | 10 | This is why smart pointer type traits are made. |
11 | 11 |
|
12 | 12 | # Release Note |
13 | | -version 1.0 released. |
| 13 | +Version 1.0 released. |
14 | 14 | > https://github.com/MyeongWoonJang/cpp-smart-pointer-type-trait/releases/tag/release |
15 | 15 |
|
16 | 16 |
|
17 | 17 | # Installation |
18 | 18 | All you need to to is copy over the smart_pointer_type_trait.hpp header file. |
19 | | -include it in your solution, and you are good to go. |
| 19 | +Include it in your solution, and you are good to go. |
20 | 20 |
|
21 | 21 |
|
22 | 22 | # Guide |
23 | | -All evaluations occurs after removing const, volatile, reference. |
| 23 | +All evaluations occurs after removing const, volatile, reference. |
| 24 | +Implementation is in namespace woon2. |
| 25 | +(Use like woon2::is_smart_ptr<T>, or write using namespace woon2; in your code.) |
24 | 26 |
|
25 | 27 | ## Hard Type Traits |
26 | | -* is_ptr<T> |
| 28 | +* is_pointable<T> |
27 | 29 | ```c++ |
28 | 30 | template < typename T > |
29 | | -struct is_ptr : is_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {}; |
| 31 | +using is_pointable = detail::is_pointable_impl< detail::remove_cvr_t< T > >; |
30 | 32 | ``` |
31 | | -```is_ptr<T>::value```/```is_ptr_v<T>``` is ```true``` if ```T``` is ```std::shared_ptr``` or ```std::unique_ptr``` or a raw pointer. |
| 33 | +```is_ptr<T>::value```/```is_ptr_v<T>``` is ```true``` if ```T``` has ```operator*``` and ```operator->```. |
| 34 | +detects all pointable classes such as ```std::unique_ptr<T>```, ```std::shared_ptr<T>```, ```T*```. |
32 | 35 |
|
33 | 36 | * is_smart_ptr<T> |
34 | 37 | ```c++ |
35 | 38 | template < typename T > |
36 | | -struct is_smart_ptr : is_smart_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {}; |
| 39 | +using is_smart_ptr = std::conditional_t< |
| 40 | + is_shared_ptr_v< T >, |
| 41 | + std::true_type, |
| 42 | + std::conditional_t< |
| 43 | + is_unique_ptr_v< T >, |
| 44 | + std::true_type, |
| 45 | + std::false_type |
| 46 | + > |
| 47 | + >; |
37 | 48 | ``` |
38 | 49 | ```is_smart_ptr<T>::value```/```is_smart_ptr_v<T>``` is ```true``` if ```T``` is ```std::shared_ptr``` or ```std::unique_ptr```. |
39 | 50 |
|
40 | 51 |
|
41 | 52 | * is_shared_ptr<T> |
42 | 53 | ```c++ |
43 | 54 | template < typename T > |
44 | | -struct is_shared_ptr : is_shared_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {}; |
| 55 | +using is_shared_ptr = detail::is_shared_ptr_impl< detail::remove_cvr_t< T > >; |
45 | 56 | ``` |
46 | 57 | ```is_shared_ptr<T>::value```/```is_shared_ptr_v<T>``` is ```true``` if ```T``` is ```std::shared_ptr```. |
47 | 58 |
|
48 | 59 | * is_unique_ptr<T> |
49 | 60 | ```c++ |
50 | 61 | template < typename T > |
51 | | -struct is_unique_ptr : is_unique_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {}; |
| 62 | +using is_unique_ptr = detail::is_unique_ptr_impl< detail::remove_cvr_t< T > >; |
52 | 63 | ``` |
53 | 64 | ```is_unique_ptr<T>::value```/```is_unique_ptr_v<T>``` is ```true``` if ```T``` is ```std::unique_ptr```. |
54 | 65 |
|
55 | 66 |
|
56 | 67 | ## Soft Type Traits |
57 | | -* is_ptr_soft<T> |
58 | | -```c++ |
59 | | -template < typename T > |
60 | | -using is_ptr_soft = decltype( is_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) ); |
61 | | -``` |
62 | | -```is_ptr_soft<T>::value```/```is_ptr_soft_v<T>``` is ```true``` if ```T``` is derived from ```std::shared_ptr``` or ```std::unique_ptr``` or is a raw pointer. |
63 | | - |
64 | 68 | * is_smart_ptr_soft<T> |
65 | 69 | ```c++ |
66 | 70 | template < typename T > |
67 | | -using is_smart_ptr_soft = decltype( is_smart_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) ); |
| 71 | +using is_smart_ptr_soft = decltype( detail::is_smart_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) ); |
68 | 72 | ``` |
69 | 73 | ```is_smart_ptr_soft<T>::value```/```is_smart_ptr_soft_v<T>``` is ```true``` if ```T``` is derived from ```std::shared_ptr``` or ```std::unique_ptr```. |
70 | 74 |
|
71 | 75 | * is_shared_ptr_soft<T> |
72 | 76 | ```c++ |
73 | 77 | template < typename T > |
74 | | -using is_shared_ptr_soft = decltype( is_shared_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) ); |
| 78 | +using is_shared_ptr_soft = decltype( detail::is_shared_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) ); |
75 | 79 | ``` |
76 | 80 | ```is_shared_ptr_soft<T>::value```/```is_shared_ptr_soft_v<T>``` is ```true``` if ```T``` is derived from ```std::shared_ptr```. |
77 | 81 |
|
78 | 82 | * is_unique_ptr_soft<T> |
79 | 83 | ```c++ |
80 | 84 | template < typename T > |
81 | | -using is_unique_ptr_soft = decltype( is_unique_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) ); |
| 85 | +using is_unique_ptr_soft = decltype( detail::is_unique_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) ); |
82 | 86 | ``` |
83 | 87 | ```is_unique_ptr_soft<T>::value```/```is_unique_ptr_soft_v<T>``` is ```true``` if ```T``` is derived from ```std::unique_ptr```. |
84 | 88 |
|
85 | 89 | # Example |
86 | | - |
87 | | - |
| 90 | + |
| 91 | + |
| 92 | + |
88 | 93 |
|
| 94 | +# Credit |
| 95 | + ### **Woon2** |
| 96 | + |
| 97 | +# Contributing |
| 98 | + we are welcoming your contributions!😊 |
| 99 | + |
89 | 100 |
|
90 | 101 | # Reference |
| 102 | +[How to write a type trait is container or is vector](https://stackoverflow.com/questions/12042824/how-to-write-a-type-trait-is-container-or-is-vector) - stackoverflow |
| 103 | + |
| 104 | +[Concept for Smart Pointers](https://stackoverflow.com/questions/65752626/concept-for-smart-pointers) - stackoverflow |
91 | 105 |
|
92 | | -[Concept for Smart Pointers](https://stackoverflow.com/questions/65752626/concept-for-smart-pointers) |
93 | | - |
94 | | -[씹어먹는 C++ - <16 - 3. 타입을 알려주는 키워드 decltype 와 친구 std::declval>](https://modoocode.com/294) |
| 106 | +[씹어먹는 C++ - <16 - 3. 타입을 알려주는 키워드 decltype 와 친구 std::declval>](https://modoocode.com/294) - blog |
95 | 107 |
|
96 | | -[std::conditional](https://en.cppreference.com/w/cpp/types/conditional) |
| 108 | +[std::conditional](https://en.cppreference.com/w/cpp/types/conditional) - cppreference |
| 109 | + |
| 110 | +[SFINAE](https://en.cppreference.com/w/cpp/language/sfinae) - cppreference |
0 commit comments