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

实测 LP64 平台基本类型宽度与极值、枚举别名、C++11 容器及四种显式类型转换

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-data-types.cpp
explore-cpp-data-types.cpp167 行
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <initializer_list>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <typeinfo>
#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 Base {
public:
    virtual ~Base() = default;
};

class Derived : public Base {
public:
    std::string show() const { return "Derived"; }
};

class Other : public Base {};

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

    // Case 1: Built-in type sizes & numeric_limits on Linux x86_64 LP64
    bool sizes_ok = (sizeof(bool) == 1) && (sizeof(char) == 1) && (sizeof(short) == 2) &&
                    (sizeof(int) == 4) && (sizeof(long) == 8) && (sizeof(long long) == 8) &&
                    (sizeof(float) == 4) && (sizeof(double) == 8) && (sizeof(wchar_t) == 4) &&
                    (sizeof(char16_t) == 2) && (sizeof(char32_t) == 4) &&
                    (std::numeric_limits<int>::max() == 2147483647) &&
                    (std::numeric_limits<unsigned short>::max() == 65535);
    cases.push_back({
        "builtin_sizes_and_limits",
        lab::jbool(true),
        lab::jbool(sizes_ok),
        sizes_ok
    });

    // Case 2: typedef / using aliases and enum auto-increment values
    typedef int feet;
    using MyInt = int;
    feet dist = 12;
    MyInt delta = 8;
    enum color { red, green = 5, blue };
    bool alias_enum_ok = (dist + delta == 20) && (red == 0) && (green == 5) && (blue == 6);
    cases.push_back({
        "typedef_using_and_enum_values",
        lab::jbool(true),
        lab::jbool(alias_enum_ok),
        alias_enum_ok
    });

    // Case 3: C++11 auto, decltype, nullptr, initializer_list, tuple & STL types
    auto x = 10;
    decltype(x) y = 20;
    int* ptr = nullptr;
    std::initializer_list<int> ilist = {1, 2, 3};
    std::tuple<int, double, char> tup(1, 2.5, 'a');
    std::array<int, 3> arr = {1, 2, 3};
    std::map<int, std::string> mp = {{1, "one"}};
    std::set<int> st = {3, 1, 2, 2};
    bool cpp11_ok = (x + y == 30) && (ptr == nullptr) && (ilist.size() == 3) &&
                    (std::get<2>(tup) == 'a') && (arr[1] == 2) && (mp[1] == "one") && (st.size() == 3);
    cases.push_back({
        "cpp11_types_and_stl_containers",
        lab::jbool(true),
        lab::jbool(cpp11_ok),
        cpp11_ok
    });

    // Case 4: static_cast, dynamic_cast (ptr + ref failure), legal const_cast, safe bit reinterpretation
    int iv = 10;
    float fv = static_cast<float>(iv);

    Derived d;
    Base* bp = &d;
    Derived* dp = dynamic_cast<Derived*>(bp);
    Other o;
    Base& bad_ref = o;
    bool caught_bad_cast = false;
    try {
        (void)dynamic_cast<Derived&>(bad_ref);
    } catch (const std::bad_cast&) {
        caught_bad_cast = true;
    }

    int mutable_origin = 7;
    const int& cref = mutable_origin;
    const_cast<int&>(cref) = 15;

    std::uintptr_t addr_int = reinterpret_cast<std::uintptr_t>(&mutable_origin);
    int* back_ptr = reinterpret_cast<int*>(addr_int);
    float fbits = 1.0f;
    std::uint32_t ieee_bits = 0;
    std::memcpy(&ieee_bits, &fbits, sizeof(ieee_bits));

    bool casts_ok = (fv == 10.0f) && (dp != nullptr && dp->show() == "Derived") &&
                    caught_bad_cast && (mutable_origin == 15) &&
                    (back_ptr == &mutable_origin) && (ieee_bits == 0x3f800000u);
    cases.push_back({
        "four_cpp_casts_and_safe_bits",
        lab::jbool(true),
        lab::jbool(casts_ok),
        casts_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-data-types-lab/explore-cpp-data-types.cpp

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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