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

实测 C++17 引用同址别名、const 右值绑定与数组引用、不可改绑语义及引用传参/返回

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

static void swap_by_ref(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}

static double& element_at(double (&arr)[4], int idx) {
    return arr[idx];
}

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

    // Case 1: Reference alias shares identical memory address with target
    int i = 5;
    double d = 11.7;
    int& r = i;
    double& s = d;
    r = 17;
    bool ok1 = (i == 17) && (r == 17) && (&r == &i) && (s == 11.7) && (&s == &d);
    cases.push_back({
        "reference_alias_and_same_address",
        lab::jbool(true),
        lab::jbool(ok1),
        ok1
    });

    // Case 2: const reference binds to rvalue; reference to array preserves size
    const int& cr = 40 + 2;
    int nums[5] = {1, 2, 3, 4, 5};
    int (&arr_ref)[5] = nums;
    arr_ref[2] = 30;
    bool ok2 = (cr == 42) && (sizeof(arr_ref) == sizeof(nums)) && (nums[2] == 30);
    cases.push_back({
        "const_ref_and_array_reference",
        lab::jbool(true),
        lab::jbool(ok2),
        ok2
    });

    // Case 3: Reference cannot rebind (assignment copies value), whereas pointer can rebind
    int a = 10, b = 25;
    int& ref_a = a;
    ref_a = b; // assigns b's value into a; ref_a still aliases a
    int* ptr = &a;
    ptr = &b;
    *ptr = 99;
    bool ok3 = (a == 25) && (&ref_a == &a) && (b == 99) && (ptr == &b);
    cases.push_back({
        "reference_vs_pointer_semantics",
        lab::esc("a=25,b=99"),
        lab::esc("a=" + std::to_string(a) + ",b=" + std::to_string(b)),
        ok3
    });

    // Case 4: Reference parameter (swap) and reference return value (assignable lvalue)
    int p = 100, q = 200;
    swap_by_ref(p, q);
    double vals[4] = {1.5, 2.5, 3.5, 4.5};
    element_at(vals, 1) = 20.5;
    bool ok4 = (p == 200) && (q == 100) && (vals[1] == 20.5);
    cases.push_back({
        "ref_parameter_and_ref_return",
        lab::jbool(true),
        lab::jbool(ok4),
        ok4
    });

    lab::emit(cases);
    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-references-lab/explore-cpp-references.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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