Advertisement Jump to content
Sign in to follow this  

Inline Variant Switch? Macro/Function?

This topic is 967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently begun my first dealings with a variant type. My only gripe with the pattern is that I would need to create a switch anywhere I use it to select the right data member from the union. I was going to make a macro as it would evade running code every time I go to use the data, but then quickly realized that was doomed to failure due to being a preprocessor directive in the first place. Although I could just implement all the data selection code within the macro every time I need to use it, or use auto assignment with the macro. auto p = myvariant.MacroGetData(); However then the issue is how to get the macro to compile inline at all. So then perhaps I could pass in a variable to the macro to assign the correct data to, but then how do I know what variable type to create.


I am having some difficulty thinking of a solution, if one even exists.


Has anybody encountered a variant type which simplifies the data selection down to a function-like call to be made inline?

Share this post

Link to post
Share on other sites

Usually I try to write code that already knows the type of data it's dealing with. So, at compile-time the type is known and just be directly casted. If the type is not known at compile-time then a run-time solution must be used, which means some kind of small performance hit (perhaps negligibly small). Usually this would be called run-time polymorphism. Good example of compile-time polymorphism is function overloading, or C++ templates (and template specialization), or any other code-generation tool.


Some options to implement run-time polymorphism come to mind:

  • function pointers for polymorphism
  • switch statements
  • jump table (enums/function pointers/virtual dispatch)

So I'd recommend trying to make sure a compile-time option will not work for you. If not then look into using some kind of run-time method. Good candidates for run-time polymorphism are pieces of code that do a common algorithmic task, but can operate on different kinds of data:

  • Serializing data
  • Settor/Gettor for member variables
  • Conversions from one type to another
  • etc.

So then we could write something like:

var a = 10;
var b = 12.0f;

printf( "%s/n", a.ToString( ) ); // "10"
printf( "%s/n", b.ToString( ) ); // "10.000000f"

Where ToString can be a polymorphic procedure. Inside of ToString we can use switches, or a jump table, or any other means of implementing polymorphism at run-time.

Edited by Randy Gaul

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!