C++: How can i create a function that accepts concatenated strings as parameter?

前端 未结 4 1341
猫巷女王i
猫巷女王i 2021-01-18 14:34

Can i design my logging-function in a way, that it accepts concatenated strings of the following form using C++?

int i = 1;
customLoggFunction(\"My Integer i         


        
4条回答
  •  [愿得一人]
    2021-01-18 15:14

    For trivial projects this is one of the few things I use a MACRO for. You can do something like this:

    #define LOG(m) do{ std::cout << timestamp() << ": " << m << '\n'; }while(0)
    
    // ...
    
    LOG("error: [" << errno "] " << filename << " does not exist.");
    

    Generally MACROS should be avoided but there is no other way to get precisely this with a standard function. So...

    Note: The empty condition do{...}while(0) enables you to place the MACRO in places that a MACRO usually can't go if it contains multiple statements.

提交回复
热议问题