C ++ - Template Specialization and Partial Specialization

I look all over the internet and stackoverflow for a specific answer, but I cannot find it. I have to create a generic class and then implement certain functions. My specific instructions were: you need to use template expression options and specialization of template classes and partial specialization.

I have a template class:

template <class T, int x, int y> class Z { T **array[x][y]; public: Z(); void print(); //and other methods }; 

I need:

1) Only Z, where x = 2 and y = 2, should have a public method void J ()

2) For char Z x = 2 and y = 2 J will do something; for everything else he does something else

3) Only for Z, where T is char, the array will be initialized to some value. For everything else it's 0

Naturally, this works:

 template<class T, int x, int y> Z<T,x,y>::Z<T,x,y>() { //initialize to 0 } 

But this is not so:

 template<int x, int y> Z<char,x,y>::Z<char,x,y>() { //initialize to something} 

And similarly (suppose J exists) this does not work:

 template <class T> void Z<T,2,2>::J() { //something } 

My question is:

Is there any simple way to implement the above elements? I need to save all other methods in Z. Giving a hint or pointing in the right direction (maybe I missed the question since there are a lot) would be helpful.

Thanks.

+3
source share
2 answers

It seems that you want to define only some functions of some specializations: if print() does not change between the char specialization and the general case, it seems that you do not want to override it.

 // What you want to do (illegal in C++) template<int,typename T> struct Z { T myValue; Z(); void print() { /* ... */ } }; template<int i, typename T> Z<i,T>::Z() { /* ... */ } template<int i> Z<i,char>::Z() { /* ... */ } 

However, this does not work. Partial or full specialization of the class has nothing to do except the 'prototype' template parameters :

 // The two following types have only two things related: the template parameter is an int, // and the second type is a full specialization of the first. There are no relations between // the content of these 2 types. template<int> struct A {}; template<> struct A<42> { void work(); }; 

You must declare and define each (partial) specialization:

 // Fixed example template<int,typename T> struct Z { T myValue; Z(); void print() { /* ... */ } }; template<int i, typename T> Z<i,T>::Z() { /* ... */ } // Specialization for <all-ints,char> template<int i> struct Z<i,char> { char myValue; char othervalue; Z(); void print() { /* Same code than for the general case */ } }; template<int i> Z<i,char>::Z() { /* ... */ } 

The only way to avoid code duplication is to use feature inheritance:

 // Example with the print function template<typename T> struct print_helper { void print() { /* ... */ } }; // Fixed example template<int,typename T> struct Z : public print_helper<T> { T myValue; Z(); }; template<int i, typename T> Z<i,T>::Z() { /* ... */ } // Specialization for <all-ints,char> template<int i> struct Z<i,char> : public print_helper<char> { char myValue; char othervalue; Z(); }; template<int i> Z<i,char>::Z() { /* ... */ } 

You cannot do what you want without duplication at the moment (the function of removing duplicate static if code was proposed for the next C ++ standard, see n3322 and n3329 ).

+4
source

You can take a look at this course http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-5-of-n

While it is not possible to define partial specialization for function templates, you can define partial specialization for a class or structure template.

 template<typename T> struct helper { static void doThingy(){} }; template<typename X> struct helper<X*> { static void doThingy(){} }; Helper(double*)::doThingy(); 

In this example, you want to specialize behavior in doThingy () only when the type in the template is a pointer type. In this case, doThingy () method overloading cannot be used. This is because you cannot overload a function without arguments. But you may have a partial specialization in structure helper. In a custom template, you performed the desired behavior for doThingy ().

0
source

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


All Articles