c++dword

is it possible to cast Dword to Type Name?


Let's take a look at this kind of code :

// 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");

Of course this kind of code is not working and it's actually useless. But it is possible to do something like that? cast a dword to a type name like int?


Solution

  • If GetValueTypeNo can be made a constexpr function, you can make something like this:

    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;
    

    and then write:

    template<dword type>
    Type<type> GetValue(...)
    { ... }
    
    constexpr dword t = GetValueTypeNo<int>();
    int test5 = myVector.GetValue<t>("test5");