It is well known that you can use macros to create a version printfthat can be removed from code at compile time (say, if you want to print only on debug builds). The resulting code can be used in the same way as used printf.
Is it possible to create a similar scenario with respect to a stream?
For example, suppose I have the following code:
#include <iostream>
class Foo
{
public:
template <typename T>
Foo& operator <<(const T& input)
{
std::cout << input;
}
};
int ComputeExpensiveThing();
void doSomething()
{
Foo() << "Expensive thing: " << ComputeExpensiveThing() << std::endl;
}
Is there a way to conditionally delete the first line of doSomething () at compile time?
I can use a macro to get a similar effect by checking the global condition at runtime:
#define FOO if(!someGlobalCondition); else Foo()
void doSomething()
{
FOO << "Expensive thing: " << ComputeExpensiveThing() << std::endl;
}
, FOO. ComputeExpensiveThing, .
, -
void doSomething()
{
Foo("Expensive thing: " << ComputeExpensiveThing() << std::endl);
}
.