I have a template class, let’s call it foo. In the implementation, I have:
template<class a, class b ... class x>
void foo<a, b, ..., x>::function1() { }
template<class a, class b ... class x>
void foo<a, b, ..., x>::function2() { }
Now I want to make it simpler to read. Could I do something like typedef to
define a THING that is
template<class a, class b... class x> foo<a, b, ..., x>
then make the above function header simpler (shorter)?
THanks!
The first question is, does your class template really need that many parameters? If so, can they be packed up in some way?
Second, if the boilerplate of defining all of the functions out-of-line is overwhelming the actual code, why not define them inline, in the class definition? Yes, some people consider that generally bad style, but it’s a tradeoff—the goal of that guideline is to make your code more readable, and if it’s having the opposite effect, ignore it.
Even if you want to keep the implementation in a separate
.ippfile that gets included at the end of the.hppfile for better code organization, you can change that to including the.ippin the middle:foo.hpp:
foo_impl.ipp:
If neither of those ideas appeals to you, there’s no way a typedef or template alias could help, because that’s for giving names to specializations of templates. No matter what you do, any template parameters that are still free, still have to be listed, or the compiler won’t know what you’re talking about.
But you could always use the preprocessor:
There are lots of ways you could modify this—put the
::in the macro, break it into two macros so you don’t need to pass in the rettype, define a different macro for each type, etc. Whatever looks most reasonable for your use case, do that.