I’m stuck with C++03 for now, and I want to create a global function that accepts any number of type-safe arguments (up to a reasonable limit if necessary, like 9).
I have access to the full boost library in my code base, so I’m hoping boost::mpl::vector can be useful here. I also don’t want this to be too inconvenient to write. Syntax at the call site should be simple like so:
LogDebugMessage("Number of cats and dogs:", m_myPets->NumCats(), m_myPets->NumDogs());
What would be the best way to implement this, in a type safe way?
EDIT
I also realize I could use template specialization for this, but I don’t want to end up defining the same struct 9 times, one for each additional template parameter. That’s just too messy for this. I’d like to avoid that if at all possible.
The best way would be 9 overloads. 😛
The easiest way for you, however, would rather be
boost::tupleinstead of usingboost::mpl, sincemplis mostly compile-time only. The call site (user) would then write something liketiecreates a tuple of references. Or if the call involves temporaries:If the logged types are a bit heavier (
boost::make_tuplemakes copies), you can resort to good oldboost::ref.Your
LogDebugMessagewould then look something like this:And after that you’d unpack the tuple using recursion similar to my tuple printer. Note that only
operator<<actually uses variadic templates, and only does so to just pick upstd::tuple. You would most likely only use theprint_tuplepart.