Files
CPP_Core/3rd/magic_enum/remove_const.hpp
T
2026-06-16 10:56:40 +08:00

172 lines
4.0 KiB
C++

#pragma once
#include <type_traits>
#include <cstddef>
// ============================
// 1. 递归去除每一层顶层 const
// 效果:深度去除所有 const
// ============================
template <class T>
struct remove_recursive_top_const {
using type = std::remove_const_t<T>;
};
template <class T>
using remove_recursive_top_const_t =
typename remove_recursive_top_const<T>::type;
// pointer
template <class T>
struct remove_recursive_top_const<T*> {
using type = remove_recursive_top_const_t<T>*;
};
template <class T>
struct remove_recursive_top_const<T* const> {
using type = remove_recursive_top_const_t<T>*;
};
template <class T>
struct remove_recursive_top_const<T* volatile> {
using type = remove_recursive_top_const_t<T>* volatile;
};
template <class T>
struct remove_recursive_top_const<T* const volatile> {
using type = remove_recursive_top_const_t<T>* volatile;
};
// lvalue reference
template <class T>
struct remove_recursive_top_const<T&> {
using type = remove_recursive_top_const_t<T>&;
};
// rvalue reference
template <class T>
struct remove_recursive_top_const<T&&> {
using type = remove_recursive_top_const_t<T>&&;
};
// array with known bound
template <class T, std::size_t N>
struct remove_recursive_top_const<T[N]> {
using type = remove_recursive_top_const_t<T>[N];
};
// array with unknown bound
template <class T>
struct remove_recursive_top_const<T[]> {
using type = remove_recursive_top_const_t<T>[];
};
// member pointer
template <class M, class C>
struct remove_recursive_top_const<M C::*> {
using type = remove_recursive_top_const_t<M> C::*;
};
template <class M, class C>
struct remove_recursive_top_const<M C::* const> {
using type = remove_recursive_top_const_t<M> C::*;
};
template <class M, class C>
struct remove_recursive_top_const<M C::* volatile> {
using type = remove_recursive_top_const_t<M> C::* volatile;
};
template <class M, class C>
struct remove_recursive_top_const<M C::* const volatile> {
using type = remove_recursive_top_const_t<M> C::* volatile;
};
// ============================
// 2. 递归去除底层 const
// 保留最外层 const
// ============================
template <class T>
struct remove_recursive_low_const {
using type = T;
};
template <class T>
using remove_recursive_low_const_t =
typename remove_recursive_low_const<T>::type;
// pointer
template <class T>
struct remove_recursive_low_const<T*> {
using type = remove_recursive_top_const_t<T>*;
};
template <class T>
struct remove_recursive_low_const<T* const> {
using type = remove_recursive_top_const_t<T>* const;
};
template <class T>
struct remove_recursive_low_const<T* volatile> {
using type = remove_recursive_top_const_t<T>* volatile;
};
template <class T>
struct remove_recursive_low_const<T* const volatile> {
using type = remove_recursive_top_const_t<T>* const volatile;
};
// lvalue reference
template <class T>
struct remove_recursive_low_const<T&> {
using type = remove_recursive_top_const_t<T>&;
};
// rvalue reference
template <class T>
struct remove_recursive_low_const<T&&> {
using type = remove_recursive_top_const_t<T>&&;
};
// array
template <class T, std::size_t N>
struct remove_recursive_low_const<T[N]> {
using type = remove_recursive_top_const_t<T>[N];
};
template <class T>
struct remove_recursive_low_const<T[]> {
using type = remove_recursive_top_const_t<T>[];
};
// member pointer
template <class M, class C>
struct remove_recursive_low_const<M C::*> {
using type = remove_recursive_top_const_t<M> C::*;
};
template <class M, class C>
struct remove_recursive_low_const<M C::* const> {
using type = remove_recursive_top_const_t<M> C::* const;
};
template <class M, class C>
struct remove_recursive_low_const<M C::* volatile> {
using type = remove_recursive_top_const_t<M> C::* volatile;
};
template <class M, class C>
struct remove_recursive_low_const<M C::* const volatile> {
using type = remove_recursive_top_const_t<M> C::* const volatile;
};