写一个自定义的类型元函数的方法,当然只是为了学习,实际上这些功能stl已经内置了
#include <iostream>
#include <format>
template <typename T>
struct my_add_const
{
using type = const T;
};
template <typename T>
struct my_remove_ptr
{
using type = T;
};
template <typename T>
struct my_remove_ptr<T*>
{
using type = T;
};
int main()
{
std::cout << std::format("{}\n{}{}\n{}{}\n\n",
"Add const to a type via a custom type metafunction",
"std::is_same_v<const int, my_add_const<int>::type>: ", std::is_same_v<const int, my_add_const<int>::type>,
"std::is_same_v<int* const , my_add_const<int*>::type>: ", std::is_same_v<int* const, my_add_const<int*>::type>);
std::cout << std::format("{}\n{}{}\n{}{}\n\n",
"Remove pointer from types via a custom type metafunciton",
"std::is_same_v<const int, my_remove_ptr<int>::type>: ", std::is_same_v<const int, my_remove_ptr<int>::type>,
"std::is_same_v<int* const , my_remove_ptr<int*>::type>: ", std::is_same_v<int* const, my_remove_ptr<int*>::type>);
std::cout << std::format("{}\n{}{}\n{}{}\n\n",
"Remove pointer from types via a custom type metafunciton",
"std::is_same_v<const int, my_remove_ptr<int>::type>: ", std::is_same_v<const int, my_remove_ptr<int>::type>,
"std::is_same_v<int* const , my_remove_ptr<int*>::type>: ", std::is_same_v<int* const, my_remove_ptr<int*>::type>);
}