更新时间:2022-12-07 19:53:45
也许以下(未经测试!)代码可能是鼓舞人心的
Perhaps the following (untested!) code might be inspirational
#define report(Log) do { std::ostringstream _os; \
_os << Log << std::flush; print(_os.str()); \
throw std::runtime(_os.str()); } while(0)
,您可以将其用作 report("x ="<< x)
;
顺便说一句,您甚至可以使用
BTW, you might even pass the source location using
#define report_at(Log,Fil,Lin) do { std::ostringstream _os; \
_os << Fil << ":" << Lin << ": " << Log << std::flush; \
print(_os.str()); \
throw std::runtime(_os.str()); } while(0)
(要降低与 _os
发生碰撞的可能性,您甚至可以使用预处理器
(to lower probability of collision with _os
you might even replace all its occurrences inside the brace with _os##Lin
using preprocessor concatenation)
#define report_at_bis(Log,Fil,Lin) report_at(Log,Fil,Lin)
#define report(Log) report_at_bis(Log,__FILE__,__LINE__)
这显示了宏真正有用的情况之一.
and this shows one of the cases where a macro is really useful.