3

Please refer to the below code
Specialized function in non specialized Template class
Is it possible to write a specialized function foo, for non specialized template class MyClass [Line Number 7] ? If yes, then, what is the syntax for the same.
Regards,
Atul

Atul
  • 745
  • 3
  • 9
  • 17
  • 1
    Don't use macros for writing `template` line. It's difficult to understand. Moreover, what is your question ? Doesn't the code shown in the link, answers the same ? – iammilind Feb 22 '12 at 03:45
  • Not clear on the question. What is foo specializing? – Vaughn Cato Feb 22 '12 at 03:46
  • @ iammilind :: The question I wanted to ask is, suppose i want to implement function foo for MyClass in a different way, and i don't want to use a specialised Template class (as what i have done in the code), then is it possible ? If yes, then how to achieve it ? – Atul Feb 22 '12 at 03:47

4 Answers4

2

Look at my example below, I have tried answer your question (if I guessed right) in the simplest code possible by me:

#include <iostream>

using namespace std;

template<typename T>
class Some
{
public:
    template<typename U> void foo(U val);
};

template<typename T>
template<typename U>
void Some<T>::foo(U val)
{
    cout << "Non specialized" << endl;
}

template<>
template<>
void Some<char>::foo(char val)
{
    cout << "Char specialized" << endl;
}

int main()
{
    Some<int> t1;
    t1.foo(5);

    Some<char> t2;
    t2.foo('c');

    return 0;
}
  1. The important thing to note here is that "You cannot specialize your class and function Independently" i.e you have to specialize both at the same time as done in the example.

  2. Also, with this you lose the opportunity to specialize your class for that data type "char" in this case. (Need to confirm on this).

UPDATE :: Confirmed on point 2.

pat
  • 12,587
  • 1
  • 23
  • 52
Arunmu
  • 6,837
  • 1
  • 24
  • 46
2

This can be done if you create a full specialization of the class template. Just refer to the answer in this question: If I want to specialise just one method in a template, how do I do it?

Otherwise if you want to have a given function with the same signature have two different behaviors depending on the instantiated version of the class, and that instantiation is a partial specialization of the template class, you will have to make a separate specialization of the template class.

Keep in mind that if you want to avoid redundant code in this second case, you can always create a base template class that will have the functionality that will not change, and then create derived template classes that will contain the unique functionality necessary for each partial specialization.

Community
  • 1
  • 1
Jason
  • 31,834
  • 7
  • 59
  • 78
  • Jason, However, this does not seem to work for template class with multiple typenames. Please refer the below code http://ideone.com/fpikG – Atul Feb 23 '12 at 04:13
  • The one that doesn't work in your example is called a *partial specialization* of a class template, and is the condition I mentioned in my answer that won't work ... you'll have to create an entire definition for the partially specialized class. If you'd like to know what syntax to use, just Google or search S.O. for "partial template specialization" ... you'll find tons of information. – Jason Feb 23 '12 at 04:25
1

If you wanted to specialize MyClass< bool >::Foo, it would look like this:

template <>
void MyClass<bool>::Foo(bool A)
{
  // code goes here
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
1

If you are asking that,

(1) you want a function Foo() which doesn't take any argument and returns void inside MyClass

(2) This Foo() should be exclusive to the MyClass when the template type is bool, i.e. only for MyClass<bool>

then here is the way:

template<class Precision>
class MyClass {
  ...
public:
  ...
  void Foo ();  // don't implement here
};
...
template<>
void MyClass<bool>::Foo ()  // implementing only for 'MyClass<bool>'
{        // invoking for other 'MyClass<>' will result in compiler error
  ...
}
iammilind
  • 68,093
  • 33
  • 169
  • 336