Tag Dispatching
Last updated
Was this helpful?
Last updated
Was this helpful?
The is a widely used idiom in C++ development. It used extensively in the following chapters of this book.
Let's try to compile application in project and take a look at the code generated by the compiler.
Somewhere in the main
function:
The code generated by the compiler looks like this:
Although the Tag1
and Tag2
are empty classes, the compiler still uses integer value 0
as a first parameter to the function.
Let's try to optimise this redundant mov r0, #0
instruction away by making it visible to the compiler that the tag parameter is not used:
Somewhere in the main
function:
The code generated by the compiler looks like this:
In this case the compiler optimises away the tag parameter.
Based on the above we may make a CONCLUSION: When idiom is used, the function that receives a dummy (tag) parameter should be a simple inline wrapper around other function that implements the required functionality. In this case the compiler will optimise away the creation of tag object and will call the wrapped function directly.