Skip to content

Instantly share code, notes, and snippets.

@jharmer95
Created July 15, 2019 21:10
Show Gist options
  • Save jharmer95/72bfd5c412586aa3c97a5e0205a46183 to your computer and use it in GitHub Desktop.
Save jharmer95/72bfd5c412586aa3c97a5e0205a46183 to your computer and use it in GitHub Desktop.
Custom C++17 type traits
#pragma once
#include <type_traits>
namespace jjh
{
// Custom because 'custom' is true
class CustomClass1
{
public:
static constexpr bool custom = true;
};
// Not custom because 'custom' is false
class CustomClass2
{
public:
static constexpr bool custom = false;
};
// Custom because it inherits from a class that has 'custom'
class CustomClass3 : public CustomClass1
{
};
// Not custom because it does not have the 'custom' boolean at all
class CustomClass4
{
};
// is_custom implementation
namespace // anonymous
{
template <typename T, typename = void> struct is_custom_base : std::false_type {};
template <typename T>
struct is_custom_base<T, std::enable_if_t<T::custom>> : std::true_type {};
}
// exposes is_custom and removes const/volatile to allow any combination
template <typename T> struct is_custom : is_custom_base<std::remove_cv_t<T>> {};
// helper
template <typename T> inline constexpr bool is_custom_v = is_custom<T>::value;
// is_small implementation
namespace // anonymous
{
template <typename T, typename = void>
struct is_small_base : std::false_type {};
template <typename T>
struct is_small_base<T, std::enable_if_t<sizeof(T) == 1>> : std::true_type {};
}
// exposes is_small and removes const/volatile to allow any combination
template <typename T> struct is_small : is_small_base<std::remove_cv_t<T>> {};
// helper
template <typename T> inline constexpr bool is_small_v = is_small<T>::value;
} // namespace jjh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment