Using type names from a template class without full qualifiers?
-
Hello.
I have a lot of useful typedefs which need to be accessible from classes of my program (I have many classes, each class requires nearly all typedefs). I can make this, for example, in such way:
class MyTypes { public: typedef vector< vector<int> > mySuperType; typedef vector< vector< vector<int> > > myUltraType; //And so on... }; class MyClass: public MyTypes { //All types from MyTypes are accessible in MyClass: mySuperType x; };Of course, I can also use namespace for this. That is not a problem.
Recently my program became more complicated: nearly all of my types (defined in
MyTypes) must be templates. But typedef templates are illegal. Hopefully, all these templates depend on the same types, so I decided to convert the wholeMyTypesinto template:template<class C> class MyTypes { public: typedef vector< vector<C> > mySuperType; typedef vector< vector< vector<C> > > myUltraType; //And so on... };The problem is that old-good way of importing all types into a class is not working (the error is described here: http://www.c-plusplus.net/forum/viewtopic-var-t-is-256174-and-highlight-is-.html ):
template<class C> class MyClass: public MyTypes<C> { mySuperType x; //Error here. I do not want to write "typename MyTypes<C>::mySuperType" EVERYWHERE };I tried to use namespace template for this, but without any success. It looks like my compiler does not support namepace templates

template<class C> namespace MyTypes //Error here. { typedef vector< vector<C> > mySuperType; typedef vector< vector< vector<C> > > myUltraType; //And so on... }; template<class C> class MyClass { using namespace MyTypes<C> mySuperType x; };Is there any way to import all types from a template to a class declaration for them to be used without full qualifiers?
The only way I found is to import types one-by-one:
template<class C> class MyClass { typedef MyTypes<C>::mySuperType mySuperType; typedef MyTypes<C>::myUltraType myUltraType; //And so on.... mySuperType x; };Any suggestions? May be the whole program design is bad?
-
imo there is no way
template<class C> class MyClass { typedef MyTypes<C>::mySuperType mySuperType; typedef MyTypes<C>::myUltraType myUltraType; //And so on.... mySuperType x; };but this is not the shortest/easiest way:
template<class C> class MyClass { using MyTypes<C>::mySuperType; using MyTypes<C>::myUltraType; //And so on.... mySuperType x; };maybe there is a need for writing
typename- i am not sure...bb
-
unskilled schrieb:
template<class C> class MyClass { using MyTypes<C>::mySuperType; using MyTypes<C>::myUltraType;This is legal but doesn't declare those ids to be types, no help there.
Without enumerating all ids that you wish to "import", the compiler doesn't know about them, because it cannot possibly look them up until the actual template parameter is known. Therefore there can't be a better way than through typedefs.
-
camper schrieb:
unskilled schrieb:
template<class C> class MyClass { using MyTypes<C>::mySuperType; using MyTypes<C>::myUltraType;This is legal but doesn't declare those ids to be types, no help there.
This is interesting. I played with this code and found that identifiers
mySuperTypeandmyUltraTypeare usable as type names ifMyTypesis defined as a base class ofMyClass:template<class C> class MyClass: public MyTypes<C> { using MyTypes<C>::mySuperType; //Visual C++ magically knows that "mySuperType" is a type using MyTypes<C>::myUltraType; mySuperType x; };I think this is non-conformant behaviour of my Visual C++ compiler. But will the
typenamekeyword help? May be this code will solve a problem on GCC compiler:template<class C> class MyClass: public MyTypes<C> { using typename MyTypes<C>::mySuperType; //notice "typename" here using typename MyTypes<C>::myUltraType; mySuperType x; };?
P.S. I like the last variant because I can
#definelong text to be short:#define using_type using typename MyTypes<C>:: template<class C> class MyClass: public MyTypes<C> { using_type mySuperType; using_type myUltraType; mySuperType x; };
-
SAn schrieb:

Using a macro, I would rather implement it like this:
// Important: The typename keyword is even at typedefs required #define USING_TYPE(TYPE) typedef typename MyTypes<C>::TYPE TYPE template <class C> class MyClass : public MyTypes<C> { USING_TYPE(mySuperType); USING_TYPE(myUltraType); // ... }; #undef USING_TYPE
-
Hm.
It seriously sucks that one can't use "using namespace" inside a class definition.