c++ - 是否可以将 Dword 转换为类型名称?

标签 c++ dword

让我们看一下这样的代码:

// Return me Dword of int
dword t = GetValueTypeNo<int>();
//here trying to tell to GetValue that my template is int (since t is dword of int)
int test5 = myVector.GetValue<t>("test5");

当然,这种代码是行不通的,而且实际上是没有用的。但有可能做这样的事情吗?将 dword 转换为类型名称,例如 int

最佳答案

如果 GetValueTypeNo 可以成为 constexpr 函数,您可以制作如下内容:

template<typename T>
constexpr dword GetValueTypeNo() { ... }

template<dword>
struct Type_selector;

template<>
struct Type_selector<dword_value_for_int> {
    using Type = int;
};

template<>
struct Type_selector<dword_value_for_long> {
    using Type = long;
};

...

template<dword type>
using Type = typename Type_selector<type>::Type;

然后写:

template<dword type>
Type<type> GetValue(...)
{ ... }

constexpr dword t = GetValueTypeNo<int>();
int test5 = myVector.GetValue<t>("test5");

关于c++ - 是否可以将 Dword 转换为类型名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56337097/

相关文章:

python - OpenCV - 检查两个图像是否 100% 相同的最快方法

c++ - 非双字对齐像素到双字对齐位图

pointers - dword ptr使用困惑

assembly - `dword ptr`是什么意思?

C++友元函数

c++ - 我被字符串和数组困住了

c++ - 如何在每次循环迭代之前清除屏幕?

C++轻型可追加数组容器

python - 如何将十六进制数转换为存储在 DWORD 而不是 QWORD 中的十进制数?

c++ - 将 DWORD 写入内存只会覆盖 1 个字节而不是 4 个字节