Thread: templates and typeid

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    templates and typeid

    Hello

    Suppose I have some templated function. Is it possible to figure out which type the template is? For instance:

    Code:
    template <typename T>
    T * some_function() {
    	if (typeid(T) == someobject_type) return ...;
    	return 0;
    }
    In case it is, is this a bad programming approach?

    I want to have one function for all object types that derive from some base class instead of having separated function for each (function returns pointer to derived class).

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I want to have one function for all object types that derive from some base class instead of having separated function for each (function returns pointer to derived class).
    It sounds like you are trying to do a capability query. Why not use virtual functions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Well this is a particularly screwy example. Normally you'd check for a specific type by creating a specialization of the template function -- an overloaded version that takes the specific type. Unfortunately, your function takes no parameters, so this is impossible.

    Quote Originally Posted by l2u View Post
    I want to have one function for all object types that derive from some base class instead of having separated function for each (function returns pointer to derived class).
    Then this function should be a virtual method of the class(es), not a standalone function.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    I have a base class that all other classes derive from. Base class has member function type which returns enum type of the derived class.

    Now I want to have function that would return pointer to derived class instead of pointer to base class.

    I thought I could solve this by having one templated function.

    How should I do this then?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by l2u View Post
    I have a base class that all other classes derive from. Base class has member function type which returns enum type of the derived class.

    Now I want to have function that would return pointer to derived class instead of pointer to base class.

    I thought I could solve this by having one templated function.

    How should I do this then?
    dynamic_cast<> ?? There's no need for a function.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you have an enum you don't even need the dynamic_cast, a static_cast will do.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    I need to check some function's return after I do static_cast, thats why I thought it would be better to make specific function for this purpose so I wouldnt have to write each time 3/4 lines instead of 1.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    But your function returns 0 if the object is the wrong type, so you still have to check the return value of the function for null.
    Code:
    if (p->GetEnum() == DERIVED_CLASS)
      static_cast<Derived*>(p)->CallDerivedClassFunction();
    vs.
    Code:
    Derived* pd = some_function<Derived>(p);
    if (pd)
      pd->CallDerivedClassFunction();
    Right?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    Yeah. Though I wouldnt have to, I prefer to be careful.
    There are some other things that should be checked and maybe modified in future before static_cast so I would prefer the second variant. What do you think?

    Thanks a lot for help

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Though I wouldnt have to, I prefer to be careful.
    What do you mean you wouldn't have to? Why not? The function has to be able to return a different value if the object is of the wrong type so you always have to check the return value. If the function always succeeded, it would have no purpose.

    If you have more than just the enum to check before the static_cast, then you can put the checks into a function. I might have the function return a bool and then do the static_cast outside of it, but either way it could work. None of that requires templates or typeid, though.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    Because some_function wouldnt be called if there was no pointer to return (program wouldnt come to that point)..
    I decided to check for NULL (inside some_function) in case I'm going to use it in some different way in future.

    Why do you guys want to avoid this kind of 'solutions'?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If some_function wouldn't be called, then what is the point of having it?

    I thought the point of some_function was to check to see if you could convert to the type and then do the conversion. If you don't need to check, then just do the conversion with the static_cast and get rid of the function.

    Use an assert to make sure t hat your assumptions about the object are correct.

    If there is some other point for the some_function, can you explain it further?

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Aren't you just reinventing RTTI here, anyway?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    If some_function wouldn't be called, then what is the point of having it?

    I thought the point of some_function was to check to see if you could convert to the type and then do the conversion. If you don't need to check, then just do the conversion with the static_cast and get rid of the function.

    Use an assert to make sure t hat your assumptions about the object are correct.

    If there is some other point for the some_function, can you explain it further?
    some_function wouldnt be called on some special occasions (if the pointer container is empty), which means (in that case) I wouldnt have to check for NULL.

    But, as I said, in future I might use this function in some other place where I wont be checking if container is empty or not.

    I dont want to have 'harmful' functions. I hope you got the point. Coding makes my brain go too much into the philosophy sometimes.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by l2u View Post
    I dont want to have 'harmful' functions. I hope you got the point. Coding makes my brain go too much into the philosophy sometimes.
    It's definitely possible to plan too much for future possibilities

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Typeid and Templates
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2003, 08:55 PM