#include <cstring>
#include <iostream>
#include <string>
#define PI_APPROX 3.14159
#define MIN_VAL(a, b) (((a) < (b)) ? (a) : (b))
#define DEBUG_MODE
#define MKSTR(x) #x
#define CONCAT(a, b) a ## b
int main() {
// 1. Object-like and function-like macros
int pi_scaled = static_cast<int>(PI_APPROX * 100000 + 0.5);
int m1 = MIN_VAL(100, 30);
int m2 = MIN_VAL(2 + 3, 4);
std::string act1 = std::to_string(pi_scaled) + ":" + std::to_string(m1) + ":" + std::to_string(m2);
const std::string exp1 = "314159:30:4";
bool ok1 = (act1 == exp1);
// 2. Conditional compilation (#ifdef, #ifndef, #if 0)
std::string trace;
#ifdef DEBUG_MODE
trace += "debug_on";
#endif
#ifndef RELEASE_MODE
trace += "|release_off";
#endif
#if 0
trace += "|unreachable";
#endif
const std::string exp2 = "debug_on|release_off";
bool ok2 = (trace == exp2);
// 3. Stringizing (#) and token-pasting (##) operators
std::string s_macro = MKSTR(HELLO C++);
int xy = 100;
int pasted = CONCAT(x, y);
std::string act3 = s_macro + ":" + std::to_string(pasted);
const std::string exp3 = "HELLO C++:100";
bool ok3 = (act3 == exp3);
// 4. Predefined macros (__LINE__, __FILE__, __DATE__, __TIME__, __cplusplus)
int cur_line = __LINE__;
std::string cur_file = __FILE__;
std::string cur_date = __DATE__;
std::string cur_time = __TIME__;
bool file_ends_ok = cur_file.size() >= 28 &&
cur_file.substr(cur_file.size() - 28) == "explore-cpp-preprocessor.cpp";
bool date_ok = (cur_date.size() == 11);
bool time_ok = (cur_time.size() == 8 && cur_time[2] == ':' && cur_time[5] == ':');
bool std_ok = (__cplusplus >= 201703L);
bool ok4 = (cur_line > 30) && file_ends_ok && date_ok && time_ok && std_ok;
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"object_and_function_macros\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"conditional_compilation_branches\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << trace << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"stringize_and_token_paste\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"predefined_macros_contract\", \"expected\": true, \"actual\": "
<< (ok4 ? "true" : "false") << ", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
<< " ]\n"
<< "}\n";
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-preprocessor-lab/explore-cpp-preprocessor.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-preprocessor.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2390.zip