I would like to get the typename of a class template from another class that use a tamplate based on the first class.
I have wrote a class like so:
template<typename T>
class class_a {
...
}
I would like to do something that:
template<class class_a>
class class_b {
std::vector<class_a.T> arr;
...
}
The easiest way to do this is to have your templates "cooperate" with each other, and for
class_a
to "help" the other template:And then:
You will find this to be a pretty much standard design pattern in the C++ library itself. Most C++ containers, for example, define
value_type
, so if we changed thetypedef
tovalue_type
:And then:
Then if your
class_b
were to be instantiated using astd::list
, for example:Then your
arr
will end up beingstd::vector<char>
.An alternative that does not require this "cooperation" would be to use a helper template with specialization, in order to avoid having to explicitly declare a
typedef
alias. However this is the simplest solution unless you have some specific reason not to do things this way.