Lists of long parameter templates with universal class class definitions

If I have a template class for which I define a member function later in the file, is there a way to avoid repeating a long list of parameters? for instance

template<class tempParam1, class tempParam2, class tempParam3> class Foo { ... int Bar(int funcParam1, int funcParam2, int funcParam3); } template<class tempParam1, class tempParam2, class tempParam3> int Foo<tempParam1, tempParam2, tempParam3>::Bar(int funcParam1, int funcParam2, int funcParam3) { ... } 

Is there a way for the line of definition of this function to be so long? With many methods to determine, this makes my code hard to read.

I tried typedef like

 template<class tempParam1, class tempParam2, class tempParam3> typedef Foo<tempParam1, tempParam2, tempParam3> FooClass; int FooClass::Bar(int funcParam1, int funcParam2, int funcParam3) { ... } 

But the compiler (g ++) complained ("error: declaring the template" typedef ").

Thanks!

+4
source share
1 answer

If you define a member inside the class {} scope, you do not need to repeat the parameters of the class template.

Perhaps you can eliminate some parameters using the idiom of signs, or otherwise calculate several parameters from one.

Instead

 template< typename size_type, typename volume_type, typename width_type > 

you may have

 template< typename param_type > ... typedef typename measurement_traits< param_type >::size_type size_type; 

and etc.

C ++ 11 represents the use of declarations that are effectively "templated typedefs", but they cannot be used in the naming specifier of a function definition that you are trying to simplify.

+3
source

Source: https://habr.com/ru/post/1492184/


All Articles