Ran Wang Ran Wang - 1 month ago 7
C++ Question

How to get a type from other type (C++ template metaprogramming)

I am fairly new to c++ template metaprogramming and trying to do the following things. Let us say I have a custom template class like this

template<typename Parameter>
class MYCLASS {
public:
Parameter parameter;
//Do something with it
};


Now in order for the user to instantiate the class, he/she needs to do

#include "other.h" //A header from other libraries
MYCLASS<other::someclass> myclass; //Suppose there is a class called other::someclass in the "other.h"


I want to spare the user the problem of finding the header, and I want to try to use metaprogramming, with something like this

template<typename Type>
struct get_right_type_impl{
//Do something here
};

template<typename Type>
using get_right_type_ = get_right_type_impl<Type>::type;


where get_right_type_ should return another type. For example, it should take a double parameter and return a other::someclass, and therefore the class definition should look like

template<typename Type>
class MYCLASS {
public:
get_right_type_<Type> parameter;
//when Type==double, get_right_type_<Type> should be equivalent to other::someclass
};

//Now one can use instantiation like this
MYCLASS<double> class;


I have tried something, mostly follow this. While I think I get some of the article, I did not cook up the example which usually has the compile problem around usage of typename. After reading some answers from this forum, I get even more confused and am not sure whether this is even possible in our case, as I have not found exact solution from googling either.

Any suggestion is welcomed.

Answer
template<typename Type>
using get_right_type_ = get_right_type_impl<Type>::type;

Consider what the compiler thinks when it sees this. It has not clue whether ::type is some variable or some type alias! You need to specify to the compiler that "Hey, I am saying 'type' is a typename, so use it that way". Hence the use of typename keyword to explicitly say that what follows is a type.

Moving on, get_right_type will basically need to be a type to type map. It's fairly easy to do this.

template <typename T> struct get_right_type;
template <> struct get_right_type<double> { using type = class_for_double; }
template <> struct get_right_type<int> { using type = class_for_int; }

Consider the above type mapping are in some header say 'common_mapping.h'. You can do the following:

#include "common_mapping.h"
get_right_type<int>::type a;