using sub-classes as fake template typedef. Bad practice?

Dear all,

I find myself dealing with very long typenames, e.g.

 
std::set<boost::shared_ptr<AbstractComponent<T,OP> >, AbstractComponentLess<T,OP> >


or, even worse,

 
typename std::set<boost::shared_ptr<AbstractComponent<T,OP> >, AbstractComponentCompartor<T,OP> >::iterator


Unfortunately, typedef does not fully support templates.
An expression like

 
typedef LongTypeNameWithManyNestedTemplateParameters ShortName<T,OP>


would only work if the "values" of T and OP are known.


I am tempted to do something like this to lessen the problem:

1
2
template<class T, class OP>
class ShortName : public LongNameWithManyNestedTemplateParameters<T,OP> {};


but, even I do not know why, it smells like a bad practice.
Any better suggestion?

I believe that solving this problem not only makes the code more readable, as shorter names are there, but it would also make it easier to modify. What, e.g., if long expressions like the ones above occur several times in the code and then I realise that I must change shared_ptr into scoped_ptr? there should be a way to do it once for all.


Thanks all,
Panecasareccio

Last edited on
You are right about the last example because doing it that way will prevent you from calling any "LongNameWithManyNestedTemplateParameters" constructors which are not the default constructor (unless of course you decide to add the same constructors to the "ShortName" class that the "LongNameWithManyNestedTemplateParameters" class uses, but this is just hackish and wasteful).

This link shows a way around this both for c++11 and c++03
http://stackoverflow.com/a/2795024/2089675
Last edited on
@OP: have you tried #define ?
Last edited on
With C++11 came template aliases that work like typedefs except they can be templated.
Consider this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

//template alias
template<typename T>
using vecAlias = std::vector<T>;

typedef vecAlias<int> vecInt;

int main() {
	vecAlias<double> v;
	vecInt v2;
	return 0;
}

http://ideone.com/PfbUHz
Last edited on
@thumper: anyway, which is better: C++03 or C++11? from the speed, effectivity, and easier-to-use point of view
Topic archived. No new replies allowed.