Consider the code below:
template <typename T>
class A{
...
}
template <class U>
class B{
...
}
int main {
B<A<int>> a;
...
}
How can I get the template parameter of A (int in this case) inside B, if A<int> is the template parameter for B? I could parametrize B as follows, but I feel like I am sending an unnecessary piece of information.
template <class AA, typename T>
class B { ... }
The reason I do not simply use template <typename T>
for class B is that I have a pointer to class A inside B, and I want to use the template parameter class AA
to see if that pointer is const or not, hence have the correct type for the member pointer in B.
There are several ways, depending of that you might change:
Quick way, specialize B
Add info in
A
directly (as std containers do withvalue_type
)Use external traits to extract information from
A
(asstd::iterator_traits
) (that also allows to handle built-in types):