I'm designing a logger using a macro. I want to concatenate the incoming log message with a constant string literal. If the log message is given as a string literal, there is no problem. However, if the macro argument is of type const char*, I cannot combine it. The problem is basically below. While solving this problem, I do not want runtime cost like std::string. Because I can concatenate string literals at compile time. I don't want runtime costs because LOG will be printed continuously. How can I solve it?
void foo(const char* msg ) { std::cout << msg << '\n'; } #define TEST(message) foo("hello " message) int main() { TEST("world"); //ok. Output will be hello world. //...codes const char* msg = "message"; TEST(msg); //error } I can do it by allocating memory like std::string and filling it into a buffer. But I don't want that.
std::string) to grow only when necessary. Best of both worlds, in my opinion (and personal experience).cout, not concatenating a string ...