I wrote a template function that converts any numerical value to string :

template <class T>
string ConverttoS(T input) 
{
	std::stringstream out;
	out << input ;
	return out.str();
}

now i want to use this template function inside a class , but if i did so i would be forced to edit all the class member functions . i.e can i make a certain member function template without rewwriting the whole class.

another problem is that even if i modified the whole class i would be forced to write :
A <int> object1;
for example

can i simply put it in global domain(i mean outside the class) and then call it . but is'nt this against encapsulation

thanks alot

Recommended Answers

All 6 Replies

Did you try it?

#include <iostream>

using namespace std;

class A
{
public:
	A() { }
	template<typename T>
	void Print(T a) { cout<<a<<endl; }
};
int main()
{  
	A t;
	t.Print(3.1415f); //implicitly
	t.Print<unsigned int>(100); //explicitly

	return 0;

}

you mean like defining the function inside the class declaration , little ugly but it works. thanks.

>>little ugly
Is it?
This is the correct way to the thing what you want.
You create a class template when you want to separate the implementation of the whole class according to the template parameter. But here, this is not what you want to do.
In fact, you could templatize the class along with a different function template:

#include <iostream>

using namespace std;
template<typename C>
class A
{
public:
	A() { }
	template<typename T>
	void Print(T a) { cout<<a<<endl; }
};
int main()
{  
	A<int> t;
	t.Print(3.1415f); //implicitly
	t.Print<unsigned int>(100); //explicitly

	return 0;

}

Here we are using two templates: one for the whole class (with C as the parameter) and other for the particular function only.

commented: class+function templates together are too often overlooked. +6

I think siddhants3 although gave a good answer, mis-understood the question by I_Empire. You can do this:

#include <iostream>

using namespace std;
template<typename C>
class A
{
public:
	A() { }
	template<typename T>
	void Print(const T&);
};

template<typename C>
template<typename T>
void A<C>::Print(const T& item) 
{
   std::cout<<item<<std::endl;
}

int main()
{  
	A<int> t;
	t.Print(3.1415f); //implicitly call t.Print<float>(const float&);
	t.Print<unsigned int>(100); //explicitly

	return 0;
}

Note the use of two templates, the first is the class and the second is the function. There are explicit rules about specialization, which if you are after I recommend posting another question / reading about them in the standard / or experimenting. But I have given the general case.

Now, you have to be a little careful. As I have written it all in one file, there is no problem BUT if you separate the files, then you are going to have to have either explicit or implicit template instatiation. That means if you have a file, say Aclass.cpp and Aclass.h, which have the code and the defiintion of the class, and you use in in another file say main.cpp. There is no way for the compiler to know that it needs a print<unsigned int> for example. If the code is in the actual definition (e.g. Aclass.h) then there is not problem.

So to get round that is automatically is (a) compiler specific, (b) dramatically increases compile time. So you can just ignore the problem and with a couple of flags to the compiler/linker you will be happy. However, it is often better to do it explicitly and then it is (a) certain to work on ANY compiler [very useful when porting stuff] (b) it compiles much faster. This is also important, more so than most beginners think. and (c) it creates earlier and better error messages.

So what to do: Consider file Aclass.cpp.

#include <iostream>
#include "Aclass.h"

template<typename T>
void A::Print(const T& item) 
{
   std::cout<<item<<std::endl;
}

// ADD THIS:template 
template class A<int>;
template void A<int>::Print(const int&);
template void A<int>::Print(const float&);

i did not really understand this part

// ADD THIS:template 
template class A<int>;
template void A<int>::Print(const int&);
template void A<int>::Print(const float&);
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.