2

I am looking to start putting in templates in my c++ class code but I have come across a situation I have not experienced before. Basically I have a non-templates class but only 1 function in the class I need to be templated.

class example
{
 public:
 example();
 ~example();
 <template T> templatefunction(T);
 nontemplatefunction(string x);
};

Is this possible? If so, is it a common solution or am I looking at templates completely in error?

user5600884
  • 119
  • 1
  • 8

1 Answers1

6

As people have noted in the comments, there's no problem doing so.

One aspect to watch out for is where to put the definition of the method templatefunction. For the time being (see the ISO cpp FAQ), you should consider placing it in the header file, which is different than what you'd probably do with the definition of the other methods. Thus you'd have example.hpp:

class example
{
 public:
 example();
 ~example();
 template<typename T> void templatefunction(T);
 void nontemplatefunction(string x);
};

template<typename T> void example::templatefunction(T)
{

}

and then example.cpp:

example::example(){}

void example::nontemplatefunction(string x)
{

}
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185