Can dynamic_cast be used to check template type?

template <class T> void checkObject(T genericObject) { MyClassA* a = dynamic_cast<MyClassA*>(genericObject); if (a != NULL) { //we know it is of type MyClassA } MyClassB* b = dynamic_cast<MyClassB*>(genericObject); if (b != NULL) { //we know it is of type MyClassB } } 

Is something like this possible? where do we have the type of the template, but we want to know its actual type?

+4
source share
3 answers

In the world of templates, you probably want to just specialize templates for each of your types instead of checking the runtime, i.e.

  template<typename T> void foo(T obj); template<> void foo<MyClassA>(MyClassA obj) { } template<> void foo<MyClassB>(MyClassB obj2) { } 

This will allow the compiler to generate the correct template at compile time, outputting your arguments.

Note this is only allowed based on the static type of the instance , that is, there is no compilation time knowledge that your variable is MyClassC , which inherits from MyClassB and therefore must use a common form, so this will not work:

  MyClassC* cinstance = new MyClassC(); foo(cinstance); //compiler error, no specialization for MyClassC 

In general, this indicates a general rule that polymorphisms of compilation time and runtime are very different systems. Templates deal exclusively with static types without knowledge of inheritance. This may surprise people from Java / C # who have a smoother integration between the two functions.

To specialize the functionality for the class at runtime, your parameters

  • Defining virtual methods may not be appropriate if this bit of functionality really needs to be part of this object.
  • Using dynamic_cast (what you are doing now) is a bit of a frown, but it might be the most direct solution that everyone gets.
  • A visitor pattern is a design pattern that uses overload to resolve a function of the correct type at run time.
+7
source

Perhaps, but MyClassA and MyClassB must have at least one virtual member function in order for dynamic_cast to work. I also think that you really want to have ( T* genericObject ) and not T genericObject in your function signature (otherwise that would make little sense).

Solutions based on specialized templates are suitable for static polymorphism , but I think the question is how to enable runtime detection for input type. I assume that the template is called with a pointer that has a type that is either a superclass of MyClassA or a superclass of MyClassB . Specialization of the template will not be able to provide the correct answer in this case.

In any case, I have a strong feeling that you are trying to do something other than achieve what you want to achieve (whatever that is). When you post such questions, I suggest you make a clear where you want to post, what is your goal; it may just be an obstacle in the wrong way.

+4
source

Yes it is possible. Note that dynamic clicks occur at runtime, and templates generate code compilation. Thus, the function will still be generated, but it will perform runtime checks for the cases you describe.

EDIT: take a look at Doug T. answer for the right way to do what you are trying to do.

+1
source

All Articles