【发布时间】:2018-08-30 13:14:40
【问题描述】:
我需要它用于我正在制作的套接字缓冲区(读取和写入......),这比其他任何东西都更像一个技巧,所以我想知道是否有更好的方法来处理当前的C++ 功能/标准?我很确定。
enum e_Types
{
Type_64 ,
Type_32 ,
Type_16 ,
Type_8 ,
Type_ALL
};
template<e_Types Type> constexpr auto _GetVarType()
{
if constexpr( Type == Type_8 )
return ( unsigned char* ) 0;
if constexpr( Type == Type_16 )
return ( unsigned short* ) 0;
if constexpr( Type == Type_32 )
return ( unsigned long* ) 0;
if constexpr( Type == Type_64 )
return ( unsigned long long* ) 0;
}
#define GetVarType(type) decltype( _GetVarType<type>() )
例子:
GetVarType( Type_64 ) p64 = malloc(0x1000);
感谢您的回复。
【问题讨论】:
-
cstdint 怎么样?还是
e_Types是必要的? -
所有命名空间都保留以
_和大写字母开头的名称
标签: c++ types enums return constexpr