4

Possible Duplicate:
partial specialization of function template

I can't find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this must been asked before, just can't find a solution.

Suppose I have a function template:

template<class any> print(any value);

I can specialize it like this for let's say a int:

template<> print<int>(int value)
{
    std::cout << value;
}

But now the problem, I want it to work with a vector as well. Since the vector class is a template class it becomes difficult.

Specializing the function like this:

template<class any> print<vector<any> >(vector<any> value) {}

Will generate the following error (MinGW g++):

FILE: error: function template partial specialization 'print<vector<any> >' is not allowed

Note that the function print is just an example.

How can I solve this?

Community
  • 1
  • 1
Tim
  • 5,521
  • 8
  • 36
  • 69

3 Answers3

5

There is a general workaround in which the function-template just delegates the job to class template member functions:

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};
template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}


int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

However, if you can come by with simple function overloading (as suggested by ecatmur and Vaughn Cato), do so.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • I confronted alot of problems using the overload eventualy, this works for me thanks! – Tim Nov 08 '12 at 16:26
  • Does it has to be specialized in the **same** file? Getting a lot of errors right now.. – Tim Nov 08 '12 at 17:39
  • It does not have to be, but typically it is better. Spreading specializations over different files that are not guaranteed to be included together, it may give you some pain, especially when you don't have much experience with templates. – Sebastian Mach Nov 09 '12 at 09:41
  • Thank you for your response. By what time the do the templates specializations have to be included? By the time it is used in a defined function, or by the time before the `int main() {...}` is defined? **Edit:** And why can I specialize function templates in other units and class templates not!? – Tim Nov 09 '12 at 11:55
2

Don't try to specialize function templates. Use overloading instead

void print(int value)
{
    std::cout << value;
}

and

template<class any>
void print(vector<any> value) {}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
1

Function template partial specialization is not allowed because it would lead to one-definition-rule violations. You can usually just use an overload:

template<class any> print(vector<any> value) {}
ecatmur
  • 152,476
  • 27
  • 293
  • 366