Here’s a possible solution: Use the same name, just at different scopes.
#include <iostream>
#define LOG(x) do { log() << (x) << '\n'; } while(false)
std::ostream & log() { return std::clog; }
struct Identifier
{
std::ostream & log() { return ::log() << id << ": "; }
int id;
explicit Identifier(int n) : id(n) {}
};
struct SomeClass : Identifier
{
SomeClass() : Identifier(123) {}
void helloFunc()
{
LOG("hello"); // will print to cout: "123hello"
}
};
int main()
{
LOG("in main");
SomeClass().helloFunc();
}
1
solved *this emulation in standalone functions [closed]