I have a simple template variable code to write an argument to a stream:
#include <iostream> void tostream(std::ostream& os) { } template<typename T, typename... Args> void tostream(std::ostream& os, const T& v, const Args&... args) { os << v; tostream(os, args...); } template<typename... Args> void log(std::ostream& os, const Args&... args) { tostream(os, args...); }
with whom I can call:
log(std::cout, "Hello", 3); log(std::cout, "Goodbye", 4);
I will compile this code with Visual Studio 2013 with all optimizations (Release config) and open the resulting executable using the IDA disassembler.
I see that the compiler created two copies of the log() function. The one that takes const char[6], int and the one that takes const char[8], int .
It also appears in the debugger when entering these functions and viewing the call stack window.
Two functions are identical except for their signature.
Is there any way to convince the compiler that these two functions should actually be one function that takes const char*, int , and not two functions?
This is a problem for me, as I have hundreds of these features inflating the size of my executable, most of which could have been avoided.
There would still be many instances of the function for different combinations of arguments, but there would be much fewer, since I only have very few possible combinations of arguments.
One thing that would allow this is to call the function as follows:
log(cout, (const char*)"Hello", 3); log(cout, (const char*)"Goodbye", 4);
But this is unacceptable, since it is very cluttered with code.