Using standard class templates from a specialized class

EDIT . In fact, I didn’t have the opportunity to check any proposed solutions when I went on vacation, and by the time I returned, the people responsible for the class template made some changes that allowed me to bypass the need to use the types defined in the class template itself.

Thanks to everyone for their help.


In short - and feel free to correct my wording, the templates are still a little voodoo for me - I need to know if I can use the ( protected ) struct or #typedef defined inside the class template from my specialized class. For instance:

This is the class template:

 template<typename T> class A : public C<T> { protected: struct a_struct { /* Class template implementation, doesn't depend on T */ }; void foo( a_struct a ); }; 

Why do I need to fully specialize in T = VAL :

 template<> class A< VAL > : public C< VAL > { void foo( a_struct a ) { // My implementation of foo, different from the class template's } }; 

If I do something like this, the compiler complains that a_struct is undefined in my specialized class. I tried to specialize and inherit from the class template, but it was ... dirty.

I saw some solutions, but they all included modifying the class template, which I cannot easily do (command).

Thoughts?

+5
source share
3 answers

No, you cannot use members of the main template declaration in your class template specialization. This is because essentially the specialization of the template class declares a completely new class template, which is applied when the template arguments match the specialization.

You have two options, though, if you want to do something like your example:

  • You can specialize the function of a member of a template class. This is useful if, in fact, only one member function is special (or at least the number of member functions is limited).
  • You can bring a member declaration (-type) to a common base class.

Since you indicated in editing that you cannot change the class template itself, it is best to select a member function.

A simplified example of specializing only a member function

 template< class T> class Printer { public: struct Guard {}; void DoPrint( const T& val) { Guard g; (void)g; std::cout << val << '\n'; } }; struct Duck {}; template<> void Printer<Duck>::DoPrint( const Duck& val) { Guard g; (void)g; std::cout << "Some duck\n"; } 

Guard here is used only to demonstrate that this type is available for both primary and specialized DoPrint() implementations.

+4
source

It is not beautiful, but you can do it like this:

 template<typename T> class C { }; template<typename T> class A : public C<T> { protected: friend A<int>; // ^^^^^^ struct a_struct { /* Class template implementation, doesn't depend on T */ }; void foo( a_struct a ); }; template<> class A< int > : public C< int > { using a_struct = typename A<void>::a_struct; // ^^^^^^ void foo( a_struct a ) { // My implementation of foo, different from the class template's } }; 
+1
source

or how about re-declaring a struct a_struct in a custom template with the same functionality as the default.

I know that this may not seem very good, since you need to enter all the specialized templates. But this is the one that I can think of now.

0
source

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


All Articles