I've done an example of 'template specialization'. Here is the code:

// template specialization
#include <iostream>

using namespace std;

// class template:
template <class T>
	class mycontainer {
		private:
			T element;
		public:
			// only prototype
			mycontainer (T);
			T increase ();
	};

// class method definition
template <class T>
	mycontainer<T> :: mycontainer(T arg) {
		element = arg;
	}

template <class T>
	T mycontainer<T> :: increase () {
		return ++element;
	}
	
// class template specialization: 
template <> 
	class mycontainer <char> { // specialized for char type
		private:
			char element;
		public:
			[b]mycontainer(char arg) {
				element = arg;
			}
			char uppercase() {
				if ((element >= 'a') && (element <= 'z')) {
					element += 'A'-'a';
					return element;
				}
				return '0';
			}[/b]
	};

// 
int main () {
	mycontainer<int> myint (7);
	mycontainer<char> mychar ('j');
	//
	cout << myint.increase() << endl;
	cout << mychar.uppercase() << endl;
	//
	return 0;
}

The compiler compiled this code successfully, but when I try to move the definition of the constructor and the method outside the class, the compiler cannot compile it.

Here is the code with outside-class-definition:

// template specialization
#include <iostream>

using namespace std;

// class template:
template <class T>
	class mycontainer {
		private:
			T element;
		public:
			// only prototype
			mycontainer (T);
			T increase ();
	};

// class method definition
template <class T>
	mycontainer<T> :: mycontainer(T arg) {
		element = arg;
	}

template <class T>
	T mycontainer<T> :: increase () {
		return ++element;
	}
	
// class template specialization: 
template <> 
	class mycontainer <char> { // specialized for char type
		private:
			char element;
		public:
			// only prototype
			[b]mycontainer(char);
			char uppercase();[/b]
	};

// definition
[b]template <>
	mycontainer<char> :: mycontainer(char arg) {
		element = arg;
	}

template <>
	char mycontainer<char> :: uppercase () {
		if ((element >= 'a') && (element <= 'z')) {
			element += 'A'-'a';
			return element;
		}
		return '0';
	}[/b]

// 
int main () {
	mycontainer<int> myint (7);
	mycontainer<char> mychar ('j');
	//
	cout << myint.increase() << endl;
	cout << mychar.uppercase() << endl;
	//
	return 0;
}

The compiler generated these errors:

... error C2910: 'mycontainer<char>::{ctor}' : cannot be explicitly specialized
... error C2910: 'mycontainer<char>::uppercase' : cannot be explicitly specialized

Even when I add the 'char' between the angle, the compiler still gave errors.

template <[B]char[/B]>
	mycontainer<char> :: mycontainer(char arg) {
		element = arg;
	}

template <[B]char[/B]>
	char mycontainer<char> :: uppercase () {
		if ((element >= 'a') && (element <= 'z')) {
			element += 'A'-'a';
			return element;
		}
		return '0';
	}

With this code, The compiler generated these errors:

... error C2244: 'mycontainer<char>::{ctor}' : unable to match function definition to an existing declaration
        definition
        'mycontainer<char>::mycontainer(char)'
        existing declarations
        'mycontainer<char>::mycontainer(const mycontainer<char> &)'
        'mycontainer<char>::mycontainer(char)'
... error C2244: 'mycontainer<char>::uppercase' : unable to match function definition to an existing declaration
        TemplateSpecialization3.cpp(36) : see declaration of mycontainer<char>::uppercase'
        definition
        'char mycontainer<char>::uppercase(void)'
        existing declarations
        'char mycontainer<char>::uppercase(void)'

I use the compiler "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86", and compile the source-code with "cl /clr *.cpp" command.

Did anyone encounter this problem? Why? Is my second code incorrect or the compiler doesn't allow?

Thank you very much :)

You might try to compile that with another compiler that supports template specialization. The last I know Microsoft compilers did not support it, but that could have changed with VC++ 2008.

I think the lines:

template<>

or

template<char>

are redundant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.