Specializing a method of a template class

Hi everybody,

I'm trying to write a templetized (templated?) container class that can be sorted. No, STL doesn't work in this case for several reasons that I won't go into now.

The sort()-method should be specialized or overloaded depending on the template parameter used for the container, e.g. an container<int> needs a different sort function than a container<std::string>. So far nothing I have tried has worked and suprisingly I haven't found any tutorials for this use case. This really shouldn't be that hard, I believe but for some reason I can't manage to find a solution.

Any ideas?
Sure, look at that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>

template <typename T>
class foo
{
  public:
    void sort();
};

template <typename T>
void foo<T>::sort()
{
    std::cout << "Generic\n";
}

template <>
void foo<int>::sort()
{
    std::cout << "Overloaded\n";
}

int main()
{
    foo<double> x;
    foo<int> y;
    x.sort();
    y.sort();
}
If you want to write different sort function for different container then why don't you just do it:
1
2
3
4
5
6
7
8
9
void sort(container<int> &c)
{
  ...
}

void sort(container<std::string> &c)
{
  ...
}
or
1
2
3
4
5
6
7
8
9
void sort_int(container<int> &c)
{
  ...
}

void sort_string(container<std::string> &c)
{
  ...
}
something like that?
@coder777: Well, because that would be too easy ;-) Actually, for some reason I haven't thought of that. that would certainly be the easiest way. However, I found a nicer way in the meantime. Functors! I just pass in a functor-type as a template parameter and call that in the sort() function. Works flawlessly, dead easy and nicely expendable. That way you can define a functor pretty much anywhere and it doesn't have to know anything about the internals of the container. It just has to return a bool representing a (partial) order between to instances of the chosen type. I wonder why I didn't think of this earlier!

@MiiniPaa: Looks nice in theory but has been giving me all sorts of headaches. This is probably because my container has two template parameters, type and size. So that would be a partial specialization of a method, which is difficult, from what I know. At least I haven't been able to get it to work. Using functors works like a charm though!
It just has to return a bool representing a (partial) order between to instances of the chosen type. I wonder why I didn't think of this earlier!
Working just like standard library sort() function. It is often used idiom in standard library. You can pass functor to almost any function where comparsion is used.

So that would be a partial specialization of a method, which is difficult, from what I know
It is impossible. However you can use something like tag dispatch to make it work: http://stackoverflow.com/questions/1501357/template-specialization-of-particular-members/1501455#1501455
and, simplier: http://stackoverflow.com/questions/10284498/partial-specialization-of-a-method-in-a-templated-class
last snipped in OP post
Topic archived. No new replies allowed.