#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int max_val(int, int);
int sum_default(int a, int b = 20) {
return a + b;
}
static void by_value(int x) { x += 50; }
static void by_pointer(int* x) { *x += 50; }
static void by_reference(int& x) { x += 50; }
struct Acc {
int base = 10;
int run(int delta) const {
auto fn = [this](int d) { return this->base + d; };
return fn(delta);
}
};
int main() {
std::vector<lab::Case> cases;
// Case 1: Function prototype & max_val call
int ret = max_val(100, 200);
cases.push_back({
"function_prototype_and_max",
lab::jint(200),
lab::jint(ret),
ret == 200
});
// Case 2: Pass by value vs pointer vs reference
int v1 = 10, v2 = 10, v3 = 10;
by_value(v1);
by_pointer(&v2);
by_reference(v3);
std::string res = std::to_string(v1) + "," + std::to_string(v2) + "," + std::to_string(v3);
cases.push_back({
"pass_by_value_pointer_reference",
lab::esc("10,60,60"),
lab::esc(res),
res == "10,60,60"
});
// Case 3: Default argument value
int s1 = sum_default(100, 200);
int s2 = sum_default(100);
std::string sres = std::to_string(s1) + "," + std::to_string(s2);
cases.push_back({
"default_argument_evaluation",
lab::esc("300,120"),
lab::esc(sres),
sres == "300,120"
});
// Case 4: C++11/17 Lambda capture by value, reference, explicit return type, and [this]
int x = 3, y = 4;
auto lam = [x, &y](int k) -> int {
int z = x + y + k;
y += 10;
return z;
};
int lout = lam(5); // 3+4+5 = 12, y becomes 14
Acc acc;
int tout = acc.run(7); // 17
bool lam_ok = (lout == 12) && (y == 14) && (tout == 17);
cases.push_back({
"lambda_closures_and_this_capture",
lab::jbool(true),
lab::jbool(lam_ok),
lam_ok
});
lab::emit(cases);
return 0;
}
int max_val(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
本例需在 本地离线 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-functions-lab/explore-cpp-functions.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-functions.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2325.zip