Reputation: 4817
I have the following macro definition:
#if DEBUG
#include <iostream>
#include <ostream>
#define LOG(x) std::cout << x << std::endl;
#else
#define LOG(x) // LOG(x) is replaced with nothing in non-debug
#endif
How would an equivalent function look like that allows this?:
LOG("This is a Test message" << " with " << testVariable << " a variable");
my current implementation looks like this:
template <typename T>
inline void logD(const T& x) {
if constexpr (Debug) {
std::cout << x << std::endl;
}
};
but I get the following error:
error C2296: '<<': illegal, left operand has type 'const char [25]'
replacing <<
with +
for concatenating doesnt help either
error C2110: '+': cannot add two pointers
Upvotes: 0
Views: 427
Reputation: 4817
With the help of Mooing_Duck i made the function a vararg template and simply use the parameter pack.
template <typename ...T>
inline void logD(const T&... x) {
if constexpr (DebugBuild) {
(std::cout << ... << x) << std::endl;
}
};
you call the function with separated commas for the content.
logD("This is a ","test with a ",variable," variable");
Upvotes: 1
Reputation: 112
The first part of function argument must be a well-defined type that can be used with standard streams, e.g:
std::string testVariable = "test";
LOG(std::string("This is a Test message") + " with " + testVariable + " a variable");
Upvotes: 0