Insight to template member function specialization

Alright...I have been beating a dead horse for about a week now, and I finally found some insight. Sources around the net say that there are only 3 ways to define an explicitly specialized member function template:

Option 1 (can make for a big header file - I'm trying to avoid this):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//program.h
class CTest
{
public:
     template <typename T> void DoSomething(T value);
     template <> void DoSomething<int>(int value);
};

template <typename T> void CTest::DoSomething(T value)
{The definition for this MUST be in the header file so the compiler knows
     how to create the proper function when used...}

template <> void CTest::DoSomething<int>(int value)
{The definition for each specialization must also be in the header file,
     which in my case is over 1600 lines of code making a large header file}


Option 2: (doesn't work in vs 2010)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//program.h
class CTest
{
public:
     template <typename T> void DoSomething(T value);
     template <> void DoSomething<int>(int value);
};

//program.cpp
template <typename T> void CTest::DoSomething(T value)
{This time the definition is in the .cpp file.  This is supposed to be allowed as long as a separate declaration is listed at the bottom of the .cpp file.}

template <> void CTest::DoSomething<int>(int value)
{This code CAN work in a .cpp file, but under a certain condition.}

template void CTest::DoSomething<int>(int value);


Option 3 (for compilers that support the 'export' keyword - not vs 2010):
1
2
3
4
5
6
7
8
9
10
11
12
13
//program.h
class CTest
{
public:
     export template <typename T> void DoSomething(T value);
     export template <> void DoSomething(int value);
};

//program.cpp
export template <typename T> void CTest::DoSomething(T value)
{This time the definition is in the .cpp file.  The export keyword was designed to handle this scenario, but voted out years ago and removed from modern compilers.}
export template <> void CTest::DoSomething<int>(int value)
{Supposed to work with the export keyword}


Okay, so after toying around enough, I discovered that the reason my code has to be in the header file is because I AM CALLING OTHER TEMPLATED MEMBER FUNCTIONS INSIDE MY TEMPLATE DEFINITIONS.

Why does this happen? Can anyone suggest another way to keep my definitions in my .cpp file with vs 2010? I've checked msdn and online sources, but most sources are either outdated or don't seem to work with vs 2010.
Last edited on
Topic archived. No new replies allowed.