简体   繁体   中英

Capture __LINE__ and __FILE__ without #define

Trying to determine a "modern" implementation for the following C-style code:

#define logError(...) log(__FILE__, __LINE__, __VA_ARGS__)

Is is possible to capture this using variadic templates or something similar that does not rely on a #define ?

Desired use case:

logError( "Oh no! An error occurred!" );

Where __FILE__ , and __LINE__ are captured under the hood, but reflect the file name and line number of where logError was called from.

宏确实是您唯一的选择,至少在std::source_location使其成为标准并满足您的愿望之前。

Actually the preprocessor is the only choice when you want to work with line numbers and filenames.

For the compiler it's not possible to use line numbers and filenames as arguments for function calls (or storing them in a variable).

In my company we had the exactly same issue with logging. We ended up with an external script scanning the source files and then building proper functions to call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM