#include <iostream>
#include <string>
// 用于演示 __VA_ARGS__ 的宏
#define LOG(...) printf("[LOG] " __VA_ARGS__)
// 演示 __COUNTER__ 的用法
#define CREATE_UNIQUE_NAME(name) name##__COUNTER__
// C++20 的 source_location 示例(需要 C++20)
#if __cplusplus >= 202002L
#include <source_location>
void demo_source_location(
const std::source_location loc = std::source_location::current()) {
std::cout << "\n[C++20 source_location]\n"
<< "File: " << loc.file_name() << '\n'
<< "Line: " << loc.line() << '\n'
<< "Function: " << loc.function_name() << std::endl;
}
#endif
void demo() {
std::cout << "=== Standard Macros ===\n";
std::cout << "__LINE__: " << __LINE__ << '\n';
std::cout << "__FILE__: " << __FILE__ << '\n';
std::cout << "__DATE__: " << __DATE__ << '\n';
std::cout << "__TIME__: " << __TIME__ << '\n';
std::cout << "__cplusplus: " << __cplusplus << '\n';
std::cout << "__func__: " << __func__ << '\n';
std::cout << "\n=== Compiler Extensions ===\n";
// GCC/Clang 扩展
#if defined(__GNUC__) || defined(__clang__)
std::cout << "__FUNCTION__: " << __FUNCTION__ << '\n';
std::cout << "__PRETTY_FUNCTION__: " << __PRETTY_FUNCTION__ << '\n';
std::cout << "__COUNTER__: " << __COUNTER__ << '\n';
std::cout << "__COUNTER__ again: " << __COUNTER__ << '\n';
std::cout << "__INCLUDE_LEVEL__: " << __INCLUDE_LEVEL__ << '\n';
#endif
// MSVC 扩展
#if defined(_MSC_VER)
std::cout << "__FUNCSIG__: " << __FUNCSIG__ << '\n';
std::cout << "__FUNCDNAME__: " << __FUNCDNAME__ << '\n';
#endif
// 其他
#ifdef __STDC_VERSION__
std::cout << "__STDC_VERSION__: " << __STDC_VERSION__ << '\n';
#endif
#ifdef __TIMESTAMP__
std::cout << "__TIMESTAMP__: " << __TIMESTAMP__ << '\n';
#endif
// 演示 __VA_ARGS__
LOG("This is a log message at line %d\n", __LINE__);
// 演示 __COUNTER__
int CREATE_UNIQUE_NAME(var) = 42; // 生成类似 var__0, var__1
}
int main() {
demo();
#if __cplusplus >= 202002L
demo_source_location();
#endif
return 0;
}