Partial Specialization of Templates

Good Evening,

Just a quick question if I may as I have trawled the web to no avail...

When I create a partial specialization of a templated class, do I need to re-implement all methods or just the ones that need the new templated member?

For example, I am creating a partial specialization to handle pointers of type T and only really need to change the implementation of two or three methods, I would like to avoid re-implementing the entire class if I can to reduce code size - I can copy and paste the other methods if I need to :)

Thank you for any help that is offered.

Phil.
You have to provide all the methods.
If you only want to change the behavior of some functions
when T is a pointer (that's what I understand that you want),
I think you can get away with something like this:

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
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

template<class T>
struct IsPointer { static const bool RESULT=false; };

template<class T>
struct IsPointer<T*> { static const bool RESULT=true; };

template <class T>
struct Foo
{
    void bar()
    {
        if (IsPointer<T>::RESULT==false) bar_non_ptr();
        else bar_ptr();
    }

    void bar_non_ptr()
    {
        cout << "T is not a pointer\n";
        cout << "doing non-ptr stuff...\n" << endl;
    }

    void bar_ptr()
    {
        cout << "T is a pointer\n";
        cout << "doing ptr stuff...\n" << endl;
    }
};

int main()
{
    Foo<int>().bar();
    Foo<char*>().bar();

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.