The C++ __FILE__ macro expands to the full source file path at compile time, which can bloat log output. This post shows how to implement a __SHORT_FILE__ macro that strips the path prefix at compile time with zero runtime overhead. The key insight is that constexpr functions only guarantee compile-time evaluation when their return value is stored in a constexpr variable. The solution uses a GCC statement expression (a scope block wrapped in parentheses) to force the result of a recursive constexpr path-trimming function into a constexpr variable, ensuring the compiler embeds only the filename in the binary without any runtime function call.
Sort: