the decltype(__VA_ARGS__) in my macro compiles when only for a single argument and not when multiple arguments are passed.
What I'm trying to achieve is to invoke a variadic function based on a runtime condition. The arguments to my printer could be expensive to compute and therefore I'd like to not evaluate those when my condition is false.
Here's a minimal example below. Any comments to rephrase this question/description are also appreciated.
#include <iostream>
#include <utility>
#include <functional>
#define PRINT_IF(printer, cond, ...) if(cond) std::invoke(&Printer::print<decltype(__VA_ARGS__)>, printer, __VA_ARGS__)
// Some Fixed Printer Interface ...
class Printer{
public:
void print(){}
template <typename T, typename... Types>
void print(T&& var1, Types&&... var2)
{
std::cout << var1 << std::endl ;
print(var2...);
}
};
int main()
{
Printer p;
PRINT_IF(p, returns_false(), very_expensive_string()); // compiles and expensive operation skipped
PRINT_IF(p, true, 1); // compiles
PRINT_IF(p, true, 1, 2, 3); // doesn't compile
// The macro should also handle pointers.
PRINT_IF(&p, true, 1, 2, 3);
return 0;
}