entitledX
entitledX

Reputation: 680

Passing a template class as an argument to a function

If I define a class using a template such as

template<class K, class V>
class myclass {
...
};

is there a way to pass objects defined by myclass to functions without using a template for the function? In order words, for every function that accepts myclass objects, does it also need to be defined using template< class K, class V> ?

The main reason for this is that I would like define a set of static functions that act on myclass objects so that I may limit the scope of these functions within their cpp files and not in header files.

Upvotes: 20

Views: 23583

Answers (7)

David Hammen
David Hammen

Reputation: 33126

Make your template class inherit from some base class:

template<class K, class V>
class myclass : mybaseclass { ... };

where the base class declares the public functionality of the template as pure virtual. This way you only have one function rather than one function for each K, V combination.

Upvotes: 11

Kerrek SB
Kerrek SB

Reputation: 477514

No, you cannot. A class template is not a class -- it is only a template. Only once you plug in the template parameters do you get a type, and for each different set of parameters you get a different, unrelated type.

Perhaps it's feasible for you to run some sort of type-erasing scheme, whereby you have a single container class which contains an abstract member pointer, and for each type you instantiate a concrete derived object. (Check out Boost.any.)

A bit like this:

class MyClassHolder { };

template <typename K, typename V>
class MyClassConcrete {  };

class MyClass
{
  MyClassHolder * p;

public:
  template <typename K, typename V> init(...)  // could be a constructor
  {
    p = new MyClassConcrete<K, V>();
  }
};

Now you can make your function accept a MyClass, but you have to add enough virtual functions to MyClassHolder, implement them in MyClassConcrete and expose them in MyClass that you can realise all your desired semantics.

Upvotes: 8

Matteo Italia
Matteo Italia

Reputation: 126927

Yes, if you want your functions to be able to accept any instantiation of your template class they too must be template (typically with the same template parameters that are used for the class).

On the other hand, you can have your template class inherit from a non-template class that still allows you to operate the derived class via virtual functions. Also, to hide the type of your class and avoid riddling all your code of templates, there are the several techniques of type erasure.

Upvotes: 4

K-ballo
K-ballo

Reputation: 81399

While your reason is not entirely clear to me, yes, each function that takes a myclass as a parameter will also have to be templated on K and V. If you manage to abstract the basics, you could have each myclass< K, V > (which is a different type for each combination of K and V) inherit from a single base class that implements the functionality, or forwards it through virtual functions.

Upvotes: 1

Tony The Lion
Tony The Lion

Reputation: 63280

You could do this:

void myfunc(const myclass<int, int>& mc) {}

Only if you wanted to be able to pass any type to your myclass argument in your function, would you need to make that myfunc a template too.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799250

Due to C++'s static nature, you must instantiate a copy of the function for each (K, V) pair in the source since the compiler will have to generate code to access each pair's members differently.

Upvotes: 1

Jon
Jon

Reputation: 437734

No, you will have to make the functions templated as well (unless of course they will be limited to only working with a specific specialization of myclass).

Upvotes: 2

Related Questions