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

实测 C++17 有符号/无符号短整型 50000 转换、const/volatile/mutable 及 static 持久计数

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

class CacheExample {
public:
    explicit CacheExample(int v) : value_(v) {}
    int get_value() const { return value_; }
    void set_value(int v) const { value_ = v; }
private:
    mutable int value_;
};

static int static_counter() {
    static int count = 0;
    return ++count;
}

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

    // Case 1: signed short vs unsigned short bit pattern for 50000 -> -15536
    short unsigned int j = 50000;
    short int i = static_cast<short int>(j);
    std::string pair_str = std::to_string(i) + " " + std::to_string(j);
    cases.push_back({
        "signed_vs_unsigned_short_wrap",
        lab::esc("-15536 50000"),
        lab::esc(pair_str),
        (i == -15536) && (j == 50000)
    });

    // Case 2: Type modifiers shorthand & width relationships
    short s_val = 10;
    long l_val = 100000L;
    long long ll_val = 10000000000LL;
    unsigned long ul_val = 42UL;
    bool mod_ok = (sizeof(short) <= sizeof(int)) && (sizeof(int) <= sizeof(long)) &&
                  (sizeof(long) <= sizeof(long long)) && (s_val == 10) &&
                  (l_val == 100000L) && (ll_val == 10000000000LL) && (ul_val == 42UL);
    cases.push_back({
        "type_modifiers_width_and_range",
        lab::jbool(true),
        lab::jbool(mod_ok),
        mod_ok
    });

    // Case 3: const, volatile, mutable in const object
    const int NUM = 10;
    const int* p1 = &NUM;
    int const* p2 = &NUM;
    volatile int vnum = 20;
    vnum = vnum + 5;
    const CacheExample ex(1);
    ex.set_value(99);
    bool qual_ok = (*p1 == 10) && (*p2 == 10) && (vnum == 25) && (ex.get_value() == 99);
    cases.push_back({
        "const_volatile_and_mutable",
        lab::jbool(true),
        lab::jbool(qual_ok),
        qual_ok
    });

    // Case 4: static local persistence + C++17 standard confirms register removed
    int c1 = static_counter();
    int c2 = static_counter();
    int c3 = static_counter();
    bool st_ok = (c1 == 1) && (c2 == 2) && (c3 == 3) && (__cplusplus >= 201703L);
    cases.push_back({
        "static_persistence_and_cpp17_register",
        lab::jint(3),
        lab::jint(c3),
        st_ok
    });

    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-modifier-types-lab/explore-cpp-modifier-types.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-modifier-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章) · 含当前章节或实例 · mbskycom202612a2381.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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