473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Indexable
  3. {
  4. public:
  5.     virtual ~Indexable();
  6.     virtual void doSomething();
  7.  
  8. }
  9.  
  10.  
  11. template <class T>
  12. void myDesiredFunction(T *ptr)
  13. {
  14.     if(dynamic_cast<Indexable*>(ptr))
  15.     {
  16.          // this is a data type derived from Indexable
  17.     }
  18.     else
  19.     {
  20.          // PODS, non-polymorphic, and classes not derived from  Indexable
  21.      }
  22.  
  23.  
Mar 18 '08 #1
4 3384
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. class Indexable
  2. {
  3. public:
  4.     virtual ~Indexable();
  5.     virtual void doSomething();
  6.  
  7. }
  8.  
  9. template <class T>
  10. void myDesiredFunction(T *ptr)
  11. {
  12.      // PODS, non-polymorphic, and classes not derived from  Indexable
  13.  
  14. template <>
  15. void myDesiredFunction(Indexable *ptr)
  16. {
  17.      // this is a data type derived from Indexable
  18.  
Mar 18 '08 #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

Expand|Select|Wrap|Line Numbers
  1. class Indexable
  2. {
  3. public:
  4.     virtual ~Indexable();
  5.     virtual void doSomething();
  6.  
  7. }
  8.  
  9. template <class T>
  10. void myDesiredFunction(T *ptr)
  11. {
  12.      // PODS, non-polymorphic, and classes not derived from  Indexable
  13.  
  14. template <>
  15. void myDesiredFunction(Indexable *ptr)
  16. {
  17.      // this is a data type derived from Indexable
  18.  
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?
Mar 18 '08 #3
Banfa
9,065 Expert Mod 8TB
static_cast you derived class pointer to a base class pointer when calling the function.
Mar 18 '08 #4
fual
28
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.

http://cci.lbl.gov/~rwgk/shortcuts/b...s_base_of.html
http://www.boost.org/libs/mpl/doc/refmanual/if.html

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.
Mar 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: tthunder | last post by:
Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
10
by: richardclay09 | last post by:
Please take a look at this method: template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
5
by: Leslaw Bieniasz | last post by:
Hello, I have the following OOP design problem. I have a base class designed as an element of a list, let's say: class A { public: // some stuff
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
4
by: aaragon | last post by:
Hello all, I think the problem I'm facing is hard so I wonder if someone faced it before and if there is a simple solution to it. I am creating a program that basically takes all the required...
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.