问题描述
template<class T>
struct TypeInfo {
using value_type = is_pointer<T>::value ? T * : T;
};
这段代码只是伪代码。我想为每个指针和值找到值类型。
我会像 sizeof(TypeInfo<something>::value_type )
一样使用它。你能帮我吗?
解决方法
你可以这样做:
template<class T>
struct TypeInfo {
using value_type = std::remove_pointer_t<T>;
};
,
template<class T>
auto consteval _GetValueType()
{
if constexpr (is_pointer<T>()) {
return _GetValueType<remove_pointer_t<T>>();
}
else {
return T();
}
}
template<class T>
struct GetValueType
{
using value_type = decltype(_GetValueType<T>());
};
这适用于任何 [(\*)*] 类。