vendredi 1 avril 2016

C++ - Getting Derived type as the template parameter

I've got a little templating scenario I'm dealing with. Here's the setup for it.

class Base {

}

class Derived : Base {

};

And my use case:

void DoAThing(Base* t) {
     DoAnotherThing(*t);
}

template<typename T>
void DoAnotherThing(T& t) {
}

So what I was hoping for was that I would be able to pass a Derived type into my "DoAThing", and then when I pass it through to DoAnotherThing, the template parameter would appear as Derived. I can see that the dereferenced type definitely is resolved by typeid as Derived, but the T type of DoAnotherThing always comes through as Base. Is there any way I can manipulate this so that DoAnotherThing resolves to the derived type that I send?

Edit: Thanks for the answers so far, but it's not quite solved for my use case yet. DoAnotherThing is to actually to create a container of the Derived class, and the problem is that the container type ends up as Container rather than Container. I want DoAThing to continue taking in the Base rather than be templates for the scenarios in which it is called.

Aucun commentaire:

Enregistrer un commentaire