让我们看一下这样的代码:
// 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/