dynamic_cast inside a template function problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jfradley
    New Member
    • Mar 2008
    • 2

    dynamic_cast inside a template function problem

    Hello,

    Here's my problem. I have a template function where I'd like to set a flag if the passed in data type is derived from a particular base class. The way to typically do this is to use dynamic_cast, unfortunately with PODs and non-polymorphic types coming I can't use the dynamic_cast.

    Right now I have a work around that puts the responsibility on the programmer to know whether the input data type is derived from this class.

    Any ideas?

    Code:
    class Indexable
    {
    public:
        virtual ~Indexable();
        virtual void doSomething();
    
    }
    
    
    template <class T>
    void myDesiredFunction(T *ptr)
    {
        if(dynamic_cast<Indexable*>(ptr))
        {
             // this is a data type derived from Indexable
        }
        else
        {
             // PODS, non-polymorphic, and classes not derived from  Indexable
         }
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Create a specialisation of the template function for that type.

    A specialisation is where you write an implementation of a template function for a specific type (or set of types) that is different from the normal template function, something like this

    [CODE=cpp]
    class Indexable
    {
    public:
    virtual ~Indexable();
    virtual void doSomething();

    }

    template <class T>
    void myDesiredFuncti on(T *ptr)
    {
    // PODS, non-polymorphic, and classes not derived from Indexable
    }

    template <>
    void myDesiredFuncti on(Indexable *ptr)
    {
    // this is a data type derived from Indexable
    }
    [/CODE]

    Comment

    • jfradley
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by Banfa
      Create a specialisation of the template function for that type.

      A specialisation is where you write an implementation of a template function for a specific type (or set of types) that is different from the normal template function, something like this

      [CODE=cpp]
      class Indexable
      {
      public:
      virtual ~Indexable();
      virtual void doSomething();

      }

      template <class T>
      void myDesiredFuncti on(T *ptr)
      {
      // PODS, non-polymorphic, and classes not derived from Indexable
      }

      template <>
      void myDesiredFuncti on(Indexable *ptr)
      {
      // this is a data type derived from Indexable
      }
      [/CODE]
      I did try that. But when passing derived data types the non-specialized template function gets called instead of the specialized. Is there a way to force it to check the specialized function first even for these cases?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        static_cast you derived class pointer to a base class pointer when calling the function.

        Comment

        • fual
          New Member
          • Feb 2008
          • 28

          #5
          Originally posted by jfradley
          I did try that. But when passing derived data types the non-specialized template function gets called instead of the specialized. Is there a way to force it to check the specialized function first even for these cases?
          What you need is a little boost magic.




          What you can do in this case is get the "if" object to return an empty function object if the input isn't derived from your base, and it will return the function object that does whatever it is you want it to if the input is derived. Since this is all done at compile time it is pretty efficient I reckon.

          If you don't have boost installed, then you will probably find it pretty useful generally so it is well worth it.

          Comment

          Working...