vendredi 5 avril 2019

Continue appending [to stream] in for-loop without local variable access

Imagine you have the following code where logDebug() is expensive or is not appropriate to call more than once:

QDebug d = logDebug();
d << __FUNCTION__ << ":";
d << "positions separated with \" --- \":";
for (const auto& str : positions)
{
    d << "---" << str;
}

A macro (just to replace the function name correctly) already exists which replaces the first 2 lines:

#define LOG_FUNCTION  this->logDebug() <<  __FUNCTION__ << ":"

It creates the local variable by calling logDebug(). Once called, you can only use the operator<< onto the macro. The problem is you can't attach the for loop body to logger.

Q: Is there a way I could use the macro for pasting all the positions (without calling logDebug again? I would guess this should be possible using lambdas, but I quite don't know how to. Please help, the shortest answer wins!

Aucun commentaire:

Enregistrer un commentaire