蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C++17 变量定义与 std::byte、extern 声明链接、静态零初始化与左右值类别

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-variable-types.cpp
explore-cpp-variable-types.cpp125 行
#include <cmath>
#include <cstddef>
#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

extern int ext_a;
extern int ext_b;
int func_demo();

int ext_a = 10;
int ext_b = 20;
static int static_zero_int;
static double static_zero_double;

int main() {
    std::vector<lab::Case> cases;

    // Case 1: Basic variable definitions & types (including std::byte in C++17)
    int a = 10, b = 20;
    int c = a + b;
    float f = 70.0f / 3.0f;
    std::byte z{22};
    char x = 'x';
    bool ok1 = (c == 30) && (std::fabs(f - 23.333333f) < 1e-4f) &&
               (std::to_integer<int>(z) == 22) && (x == 'x');
    cases.push_back({
        "basic_variable_definitions_and_sizes",
        lab::jbool(true),
        lab::jbool(ok1),
        ok1
    });

    // Case 2: extern variable declaration & function prototype linkage
    int sum_ext = ext_a + ext_b + func_demo();
    cases.push_back({
        "extern_declaration_and_linkage",
        lab::jint(37),
        lab::jint(sum_ext),
        sum_ext == 37
    });

    // Case 3: Static storage duration zero-initialization
    bool zero_ok = (static_zero_int == 0) && (static_zero_double == 0.0);
    cases.push_back({
        "static_zero_init_vs_explicit_init",
        lab::jbool(true),
        lab::jbool(zero_ok),
        zero_ok
    });

    // Case 4: Lvalue vs Rvalue semantics
    int g = 20;
    int* pg = &g; // lvalue has an address
    *pg = 35;
    int&& rref = 10 + 20; // rvalue reference binds to temporary rvalue
    bool lr_ok = (g == 35) && (rref == 30);
    cases.push_back({
        "lvalue_and_rvalue_categories",
        lab::jbool(true),
        lab::jbool(lr_ok),
        lr_ok
    });

    lab::emit(cases);
    return 0;
}

int func_demo() {
    return 7;
}
本例需在 本地离线 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-variable-types-lab/explore-cpp-variable-types.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-variable-types.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2403.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习