Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialization of templated member function in templated class

Tags:

I have a templated class with an templated member function

template<class T> class A { public:     template<class CT>     CT function(); }; 

Now I want to specialize the templated member function in 2 ways. First for having the same type as the class:

template<class T> template<>  // Line gcc gives an error for, see below T A<T>::function<T>() {     return (T)0.0; } 

Second for type bool:

template<class T> template<> bool A<T>::function<bool>() {     return false; } 

Here is how I am trying to test it:

int main() {     A<double> a;     bool b = a.function<bool>();     double d = a.function<double>(); } 

Now gcc gives me for the line marked above:

error: invalid explicit specialization before ‘>’ token error: enclosing class templates are not explicitly specialize 

So gcc is telling me, that I have to specialize A, if I want to specialize function, right? I do not want to do that, I want the type of the outer class to be open ...

Is the final answer: it is not possible? Or is there a way?

like image 472
Nathan Avatar asked Jul 21 '11 08:07

Nathan


People also ask

Can a non templated class have a templated member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.

What is explicit template specialization?

Explicit (full) specializationAllows customizing the template code for a given set of template arguments.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

When we specialize a function template it is called?

To do so, we can use a function template specialization (sometimes called a full or explicit function template specialization) to create a specialized version of the print() function for type double.


2 Answers

Yes, this is the problem:

error: enclosing class templates are not explicitly specialized  

You cannot specialize a member without also specializing the class.

What you can do is put the code from function in a separate class and specialize that, much like basic_string depends on a separate char_traits class. Then then non-specialized function can call a helper in the traits class.

like image 111
Bo Persson Avatar answered Sep 19 '22 11:09

Bo Persson


You can use overload, if you change the implementation.

template <typename T> class Foo { public:   template <typename CT>   CT function() { return helper((CT*)0); }  private:   template <typename CT>   CT helper(CT*);    T helper(T*) { return (T)0.0; }    bool helper(bool*) { return false; } }; 

Simple and easy :)

like image 31
Matthieu M. Avatar answered Sep 19 '22 11:09

Matthieu M.