Skip to content

Instantly share code, notes, and snippets.

@dpiponi
Created March 6, 2024 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpiponi/4a08550f3141c858f3cb912e89579b1c to your computer and use it in GitHub Desktop.
Save dpiponi/4a08550f3141c858f3cb912e89579b1c to your computer and use it in GitHub Desktop.
My own implementation of decay, just to be sure it does what I think
#include <type_traits>
template<typename T, typename U>
constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;
template<typename T>
struct MyDecay
{
static auto Helper(T x)
{
return x;
}
typedef decltype(Helper(std::declval<T>())) type;
};
template<typename T, typename U>
constexpr bool my_decay_equ = std::is_same_v<typename MyDecay<T>::type, U>;
int main()
{
static_assert
(
is_decay_equ<int, int> &&
! is_decay_equ<int, float> &&
is_decay_equ<int&, int> &&
is_decay_equ<int&&, int> &&
is_decay_equ<const int&, int> &&
is_decay_equ<int[2], int*> &&
! is_decay_equ<int[4][2], int*> &&
! is_decay_equ<int[4][2], int**> &&
is_decay_equ<int[4][2], int(*)[2]> &&
is_decay_equ<int(int), int(*)(int)>
);
static_assert
(
my_decay_equ<int, int> &&
! my_decay_equ<int, float> &&
my_decay_equ<int&, int> &&
my_decay_equ<int&&, int> &&
my_decay_equ<const int&, int> &&
my_decay_equ<int[2], int*> &&
! my_decay_equ<int[4][2], int*> &&
! my_decay_equ<int[4][2], int**> &&
my_decay_equ<int[4][2], int(*)[2]> &&
my_decay_equ<int(int), int(*)(int)>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment