Можно ли привести Dword к Type Name? - PullRequest
2 голосов
/ 28 мая 2019

Давайте посмотрим на код такого типа:

// 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?

Ответы [ 2 ]

2 голосов
/ 28 мая 2019

Если 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");
0 голосов
/ 28 мая 2019

Вы можете использовать ключевое слово decltype:

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

Но здесь параметр шаблона GetValue будет dword вместо int.

...