Skip to content

Commit d95decb

Browse files
committed
added is_ptr
1 parent a1106bc commit d95decb

File tree

2 files changed

+105
-28
lines changed

2 files changed

+105
-28
lines changed

example.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ struct Sptr : std::shared_ptr< Ty >
1010
{
1111
void func()
1212
{
13-
std::cout << "¿¹\n";
13+
std::cout << "test\n";
1414
}
1515
Sptr( const Ty& val ) : std::shared_ptr< Ty >( new Ty{ val } ) {}
1616
};
17+
1718
template < typename Ty >
1819
struct Uptr : std::unique_ptr< Ty >
1920
{
2021
void func()
2122
{
22-
std::cout << "¿¹\n";
23+
std::cout << "test\n";
2324
}
2425
Uptr( const Ty& val ) : std::unique_ptr< Ty >( new Ty{ val } ) {}
2526
};
27+
2628
int main()
2729
{
2830
auto check = []( auto&& target )
@@ -31,19 +33,29 @@ int main()
3133
std::cout << "is_shared_ptr: " << is_shared_ptr_v< decltype( target ) > << '\n';
3234
std::cout << "is_unique_ptr: " << is_unique_ptr_v< decltype( target ) > << '\n';
3335
std::cout << "is_smart_ptr: " << is_smart_ptr_v< decltype( target ) > << '\n';
34-
std::cout << "inherit_from_shared_ptr: " << inherit_from_shared_ptr_v< decltype( target ) > << '\n';
35-
std::cout << "inherit_from_unique_ptr: " << inherit_from_unique_ptr_v< decltype( target ) > << '\n';
36-
std::cout << "inherit_from_smart_ptr: " << inherit_from_smart_ptr_v< decltype( target ) > << '\n';
36+
std::cout << "is_ptr: " << is_ptr_v< decltype( target ) > << '\n';
37+
std::cout << "is_shared_ptr_soft: " << is_shared_ptr_soft_v< decltype( target ) > << '\n';
38+
std::cout << "is_unique_ptr_soft: " << is_unique_ptr_soft_v< decltype( target ) > << '\n';
39+
std::cout << "is_smart_ptr_soft: " << is_smart_ptr_soft_v< decltype( target ) > << '\n';
40+
std::cout << "is_ptr_soft: " << is_ptr_soft_v< decltype( target ) > << '\n';
3741
std::cout << "\n\n\n";
3842
};
43+
3944
const auto a = std::make_unique< int >( 3 );
4045
check( a );
46+
4147
Uptr< int > b{ 2 };
4248
check( b );
49+
4350
auto c = std::make_shared< int >( 4 );
4451
check( c );
52+
4553
Sptr< int > d{ 5 };
4654
check( d );
55+
4756
int e = 8;
4857
check( e );
58+
59+
int* f = &e;
60+
check( f );
4961
}

smart_pointer_type_trait.hpp

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,114 @@
11
// ==========================================================================
22
// type traits for smart pointers.
3-
// supporting is_unique_ptr< T >, is_shared_ptr< T >, is_smart_pointer< T >.
3+
// supporting is_unique_ptr< T >, is_shared_ptr< T >, is_smart_ptr< T >, is_ptr< T >
44
// usage is like std::is_pointer< T >.
5+
//
56
// ==========================================================================
67

78
#ifndef _smart_pointer_type_trait
89
#define _smart_pointer_type_trait
910

1011
#include <memory>
1112

12-
template < typename T > struct is_shared_ptr_impl : std::false_type {};
13-
template < typename T > struct is_shared_ptr_impl< std::shared_ptr< T > > : std::true_type {};
14-
template < typename T > struct is_shared_ptr : is_shared_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {};
15-
template < typename T > constexpr bool is_shared_ptr_v = is_shared_ptr< T >::value;
13+
// =======================================================================================
14+
// shared_ptr type trait
15+
// =======================================================================================
16+
template < typename T >
17+
struct is_shared_ptr_impl : std::false_type {};
1618

17-
template < typename T > struct is_unique_ptr_impl : std::false_type {};
18-
template < typename T, typename Dx > struct is_unique_ptr_impl< std::unique_ptr< T, Dx > > : std::true_type {};
19-
template < typename T > struct is_unique_ptr : is_unique_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {};
20-
template < typename T > constexpr bool is_unique_ptr_v = is_unique_ptr< T >::value;
19+
template < typename T >
20+
struct is_shared_ptr_impl< std::shared_ptr< T > > : std::true_type {};
21+
22+
template < typename T >
23+
struct is_shared_ptr : is_shared_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {};
24+
25+
template < typename T >
26+
constexpr bool is_shared_ptr_v = is_shared_ptr< T >::value;
27+
28+
template < typename T >
29+
std::true_type is_shared_ptr_soft_impl( const std::shared_ptr< T >* );
30+
std::false_type is_shared_ptr_soft_impl( ... );
31+
32+
template < typename T >
33+
using is_shared_ptr_soft = decltype( is_shared_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
2134

2235
template < typename T >
23-
std::true_type inherit_from_shared_ptr_impl( const std::shared_ptr< T >* );
24-
std::false_type inherit_from_shared_ptr_impl( ... );
36+
constexpr bool is_shared_ptr_soft_v = is_shared_ptr_soft< T >::value;
37+
38+
// shared_ptr type trait end ==============================================================
39+
40+
// =======================================================================================
41+
// unique_ptr type trait
42+
// =======================================================================================
2543
template < typename T >
26-
using inherit_from_shared_ptr = decltype( inherit_from_shared_ptr_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
27-
template < typename T > constexpr bool inherit_from_shared_ptr_v = inherit_from_shared_ptr< T >::value;
44+
struct is_unique_ptr_impl : std::false_type {};
2845

2946
template < typename T, typename Dx >
30-
std::true_type inherit_from_unique_ptr_impl( const std::unique_ptr< T, Dx >* );
31-
std::false_type inherit_from_unique_ptr_impl( ... );
47+
struct is_unique_ptr_impl< std::unique_ptr< T, Dx > > : std::true_type {};
48+
49+
template < typename T >
50+
struct is_unique_ptr : is_unique_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {};
51+
52+
template < typename T > constexpr bool is_unique_ptr_v = is_unique_ptr< T >::value;
53+
54+
template < typename T, typename Dx >
55+
std::true_type is_unique_ptr_soft_impl( std::unique_ptr< T, Dx >* );
56+
std::false_type is_unique_ptr_soft_impl( ... );
57+
58+
template < typename T >
59+
using is_unique_ptr_soft = decltype( is_unique_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
60+
3261
template < typename T >
33-
using inherit_from_unique_ptr = decltype( inherit_from_unique_ptr_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
34-
template < typename T > constexpr bool inherit_from_unique_ptr_v = inherit_from_unique_ptr< T >::value;
62+
constexpr bool is_unique_ptr_soft_v = is_unique_ptr_soft< T >::value;
3563

36-
template < typename T > struct is_smart_ptr : std::conditional_t< is_shared_ptr_v< T >, std::true_type,
64+
// unique_ptr type trait end ==============================================================
65+
66+
// =======================================================================================
67+
// united type trait ( smart pointer, pointer( smart pointers + raw_pointer ) )
68+
// =======================================================================================
69+
template < typename T >
70+
struct is_smart_ptr : std::conditional_t< is_shared_ptr_v< T >, std::true_type,
3771
std::conditional_t< is_unique_ptr_v< T >, std::true_type, std::false_type > > {};
72+
3873
template < typename T > constexpr bool is_smart_ptr_v = is_smart_ptr< T >::value;
3974

4075
template < typename T, typename Dx >
41-
std::true_type inherit_from_smart_ptr_impl( const std::unique_ptr< T, Dx >* );
76+
std::true_type is_smart_ptr_soft_impl( std::unique_ptr< T, Dx >* );
77+
template < typename T >
78+
std::true_type is_smart_ptr_soft_impl( std::shared_ptr< T >* );
79+
std::false_type is_smart_ptr_soft_impl( ... );
80+
81+
template < typename T >
82+
using is_smart_ptr_soft = decltype( is_smart_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
83+
84+
template < typename T >
85+
constexpr bool is_smart_ptr_soft_v = is_smart_ptr_soft< T >::value;
86+
87+
88+
template < typename T >
89+
struct is_ptr_impl : std::conditional_t< std::is_pointer_v< T >, std::true_type,
90+
std::conditional_t< is_smart_ptr_v< T >, std::true_type, std::false_type > > {};
91+
92+
template < typename T >
93+
struct is_ptr : is_ptr_impl< std::remove_cv_t< std::remove_reference_t< T > > > {};
94+
95+
template < typename T >
96+
constexpr bool is_ptr_v = is_ptr< T >::value;
97+
98+
template < typename T, typename Dx >
99+
std::true_type is_ptr_soft_impl( std::unique_ptr< T, Dx >* );
42100
template < typename T >
43-
std::true_type inherit_from_smart_ptr_impl( const std::shared_ptr< T >* );
44-
std::false_type inherit_from_smart_ptr_impl( ... );
101+
std::true_type is_ptr_soft_impl( std::shared_ptr< T >* );
45102
template < typename T >
46-
using inherit_from_smart_ptr = decltype( inherit_from_smart_ptr_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
47-
template < typename T > constexpr bool inherit_from_smart_ptr_v = inherit_from_smart_ptr< T >::value;
103+
std::true_type is_ptr_soft_impl( T** );
104+
std::false_type is_ptr_soft_impl( ... );
105+
106+
template < typename T >
107+
using is_ptr_soft = decltype( is_ptr_soft_impl( std::declval< std::remove_cv_t< std::remove_reference_t< T > >* >() ) );
108+
109+
template < typename T >
110+
constexpr bool is_ptr_soft_v = is_ptr_soft< T >::value;
111+
112+
// united type trait end ==============================================================
48113

49114
#endif

0 commit comments

Comments
 (0)