Skip to content

C++ Type Traits for Smart Pointers that are not included in the standard library, containing inheritance detection and member detection.

License

Notifications You must be signed in to change notification settings

Woon-2/cpp-smart-pointer-type-trait

Repository files navigation

Smart Pointer Type Trait🎈

A simple, header-only cpp library implementing smart pointer type traits.
You can easily compile your code diffrently depending on which pointer-type is used.
You can detect inheritance too and you won't have trouble with const, volatile qualifiers.
It also contains member detections. (SFINAE based)

Type traits are generally used for static assertions and static polymorphism. (C++ 17 if constexpr)
However, the standard library's type traits do not support smart pointers, and do not detect inheritance.
This is why smart pointer type traits are made.

Release Note

Version 2.0 released.

https://github.com/MyeongWoonJang/cpp-smart-pointer-type-trait/releases/tag/v2.0

If you are looking for old versions, visit here.

Installation

All you need to to is copy over the smart_pointer_type_trait.hpp header file.
Include it in your solution, and you are good to go.

Guide

All evaluations occurs after removing const, volatile, reference.
Implementations are in namespace woon2.

#include "smart_pointer_type_trait.hpp"		// include the header file.

// ...

	woon2::is_smart_ptr_v<T>;		// usage guide 1

	using namespace woon2;			// usage guide 2
	is_smart_ptr_v<T>;

// ...

Hard Type Traits

  • is_pointable
template < typename T >
using is_pointable = detail::is_pointable_impl< detail::remove_cvr_t< T > >;

is_ptr<T>::value/is_ptr_v<T> is true if T has operator* and operator->.
detects all pointable classes such as std::unique_ptr<T>, std::shared_ptr<T>, T*.

  • is_smart_ptr
template < typename T >
using is_smart_ptr = std::conditional_t<
	is_shared_ptr_v< T >,
	std::true_type,
	std::conditional_t<
		is_unique_ptr_v< T >,
		std::true_type,
		std::false_type
		>
	>;

is_smart_ptr<T>::value/is_smart_ptr_v<T> is true if T is std::shared_ptr or std::unique_ptr.

  • is_shared_ptr
template < typename T >
using is_shared_ptr = detail::is_shared_ptr_impl< detail::remove_cvr_t< T > >;

is_shared_ptr<T>::value/is_shared_ptr_v<T> is true if T is std::shared_ptr.

  • is_unique_ptr
template < typename T >
using is_unique_ptr = detail::is_unique_ptr_impl< detail::remove_cvr_t< T > >;

is_unique_ptr<T>::value/is_unique_ptr_v<T> is true if T is std::unique_ptr.

Soft Type Traits

  • is_smart_ptr_soft
template < typename T >
using is_smart_ptr_soft = decltype( detail::is_smart_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) );

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.

  • is_shared_ptr_soft
template < typename T >
using is_shared_ptr_soft = decltype( detail::is_shared_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) );

is_shared_ptr_soft<T>::value/is_shared_ptr_soft_v<T> is true if T is derived from std::shared_ptr.

  • is_unique_ptr_soft
template < typename T >
using is_unique_ptr_soft = decltype( detail::is_unique_ptr_soft_impl( std::declval< detail::remove_cvr_t< T >* >() ) );

is_unique_ptr_soft<T>::value/is_unique_ptr_soft_v<T> is true if T is derived from std::unique_ptr.

Member Detections

  • has_reset
template < typename T >
using has_reset = detail::has_reset_impl< detail::remove_cvr_t< T > >;	

has_reset<T>::value/has_reset_v<T> is true if T has reset() as a member.

  • has_release
template < typename T >
using has_release = detail::has_release_impl< detail::remove_cvr_t< T > >;	

has_release<T>::value/has_release_v<T> is true if T has release() as a member.

  • has_get_deleter
template < typename T >
using has_get_deleter = detail::has_get_deleter_impl< detail::remove_cvr_t< T > >;	

has_get_deleter<T>::value/has_get_deleter_v<T> is true if T has get_deleter() as a member.

  • has_swap
template < typename T >
using has_swap = detail::has_swap_impl< detail::remove_cvr_t< T > >;	

has_swap<T>::value/has_swap_v<T> is true if T has swap(detail::remove_cvr_t<T>&) as a member.
(detail::remove_cvr_t<T>& is a l-value reference of the pointer.)

Example

Smart Pointer Type Trait1 Smart Pointer Type Trait2

examples of using member detections are on other examples. (example2, example3)

Contributing

we are welcoming your contributions!😊

Credit

Woon2

Reference

How to write a type trait is container or is vector - stackoverflow

Concept for Smart Pointers - stackoverflow

씹어먹는 C++ - <16 - 3. 타입을 알려주는 키워드 decltype 와 친구 std::declval> - blog

std::conditional - cppreference

SFINAE - cppreference

About

C++ Type Traits for Smart Pointers that are not included in the standard library, containing inheritance detection and member detection.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages