When a template class is inherited from another template class, typedefs in the base class must be overridden (that is, they are not automatically inherited), and function calls in the base class must be qualified. Why is this? Isn't that already clear?
So, if I have 20 template classes, all defining the same typedefs, I cannot imagine the base class containing these definitions and inherit from it, since I have to redefine typedefs in any case in each class, which defeats the goal . This makes the source code unnecessarily verbose.
I see that this was discussed in this question , but I do not understand the comment
C ++ name lookup rules determine that a name is used only in template base classes if it depends on the template parameter (if it is a "dependent name"). If the name does not depend on the template parameter, it is not executed there.
What is the reason for this? It makes no sense to me.
Perhaps the following code snippet illustrates my question better:
#include <iostream> template <unsigned N, typename T> struct A { typedef TU; static void foo(T x) { std::cout << N + x << "\n"; } }; template <unsigned N, typename T> struct B : public A<N, T> { // Why do I have to redeclare U? Isn't is unambiguous already? typedef typename A<N, T>::UU; // why do I have to specify B::? Isn't it unambiguous already? static void zoo(U x) { B::foo(x); } }; int main() { B<2,int>().zoo(3); B<2,double>().zoo(3.5); return 0; }
c ++ typedef templates
Fabio
source share