#include <type_traits>
#include "stdio.h"
template<typename T, typename U>
constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;
int func(float num) {return num;}
struct S
{
void f_ref() & {}
void f_const() const {}
};
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)>
);
printf("1 %s\n",is_decay_equ<int[4][2], int(*)[2]>?"true":"false");
printf("2 %s\n",is_decay_equ<int(int), int(*)(int)>?"true":"false");
/**
* https://en.cppreference.com/w/cpp/types/decay
* template< class T >
struct decay;
* Performs the type conversions equivalent to the ones performed when passing function arguments by value. Formally:
1) If T is "array of U" or reference to it, the member typedef type is U*.
2) Otherwise, if T is a function type F or reference to one?, the member typedef type is std::add_pointer<F>::type.
3) Otherwise, the member typedef type is std::remove_cv<std::remove_reference<T>::type>::type.
*/
printf("3 %s\n",is_decay_equ<int(float),std::add_pointer_t<int(float)>>?"true":"false"); // true
typedef int(*funcRef)(float);
printf("4 %s\n",std::is_same_v<funcRef,std::add_pointer_t<int(float)>>?"true":"false"); // true,对应3)
printf("5 %s\n",std::is_same_v<funcRef,int(*)(float)>?"true":"false"); // true,对应3)
printf("6 %s\n",std::is_same_v<funcRef,int(float)>?"true":"false"); // false
//int(*funcRef)(float)&;
//funcRef = = func;
}
std::decay结合cppreference理解
于 2024-03-27 13:54:05 首次发布