0

If I have a generic function that takes a void* as a parameter, can Auto in C++11 help stop any bad casting of that parameter?

For instance, we may have an event system that sends events and a pointer to some data that is relevant for the event it sends.

void EventClass::SendEvent (eventTypeEnum eventType, void* eventData) { int* castEventData = static_cast<int*> (eventData); //.. Do something } 

In the above example, one eventType could be sent with eventData as a float* and another as a pointer to a structure. The static_cast there will be mis-casting and causing problems.

Is there a way auto could be used to avoid this issue.

Please note, this is just an example (not a great one though sorry) to help me understand the possibilities of auto and not an actual issue I have.

4
  • I presume you have a way to know which cast is the correct one to apply? Maybe by checking the eventType enum? Commented Aug 6, 2014 at 23:01
  • Yes, that is what I would do in a normal situation, but what I am wondering is if the 'auto' keyword from C++11 can be used to figure this out for me, so that there is no chance of mistakes being made. Commented Aug 6, 2014 at 23:07
  • auto deduces the type at compile time. What you need is deduction at run-time. One probable solution to your problem is to pass boost::any instead of void*. Commented Aug 7, 2014 at 3:05
  • We are actually using a method like boosts any in some places, I was just wondering if auto could replace it. It's not something I actually need to solve, just trying to fully understand auto. Missed the fact that it was compile time. Cheers for the quick and simple explanation. Commented Aug 7, 2014 at 8:08

1 Answer 1

4

No, remember that 'auto' is a compile time feature: it says the compiler may infer the type of a variable from it's initializer. For example, if the compiler sees

auto i = 10; 

it can infer that i is an integer. Or more usefully, given

auto it = c.begin(); 

where c is some sort of collection, the compiler can infer that it is an iterator of the appropriate subtype.

Here your function simply says that eventData is a void*, so that's all the compiler has to go on. Detecting that in a particular instance you've passed a float* rather than an int* is a run time issue and auto won't solve it.

I think some languages have used an 'auto' for a generic data type like Microsoft's 'Variant', but that's not what the 'auto' in C++ is providing.

It sounds like you need support for Run Time Type Identification (RTTI), but that still assumes some constraint on what you are passing in the function. If you truly need to be able to pass anything from a char* to a IncrediblyComplexStruct*, you probably are just going to have to pass the type along with the value, possibly using the eventTypeEnum as @RobertHarvey suggested.

1
  • Ah! Yes. I missed the whole compile time bit. Cheers for the explanation. Commented Aug 7, 2014 at 8:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.