Passing a template class as an argument to a function?

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).

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.

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 class MyClassConcrete { }; class MyClass { MyClassHolder * p; public: template init(...) // could be a constructor { p = new MyClassConcrete(); } }; 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.

Make your template class inherit from some base class: template 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.

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).

Looking for an equivalent of "static" for header files – entitledX Sep 17 at 23:13 @entitledX: Not sure what you mean about the equivalent of static, but free functions are always publicly accessible so no, declaring a function would allow any code that sees the declaration to call it. – Jon Sep 17 at 23:17 Anonymous namespaces can be used to achieve the same effect as static, not that it matters since it won't work as you want in a header file. Header file get, you know, included in the respective source files... – Staffan Sep 18 at 8:02.

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.

You could do this: void myfunc(const myclass& 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.

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 (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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions