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

实测 C++17 多进制整浮点字面量、转义序列、相邻字符串拼接及 const/constexpr 常量

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

#define LENGTH_MACRO 10
#define WIDTH_MACRO 5
#define NEWLINE_MACRO '\n'

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

    // Case 1: Decimal, octal, hex, binary (C++14) integer & float literals
    int dec_v = 85;
    int oct_v = 0213;     // 2*64 + 1*8 + 3 = 139
    int hex_v = 0x4b;     // 75
    unsigned long ul_v = 0xFeeUL; // 4078
    double f1 = 3.14159;
    long double f2 = 314159E-5L;
    bool lit_ok = (dec_v == 85) && (oct_v == 139) && (hex_v == 75) && (ul_v == 4078UL) &&
                  (std::fabs(f1 - static_cast<double>(f2)) < 1e-12);
    cases.push_back({
        "integer_and_float_literals",
        lab::jbool(true),
        lab::jbool(lit_ok),
        lit_ok
    });

    // Case 2: Boolean, char, wide char, and escape sequences
    bool b_true = true;
    bool b_false = false;
    wchar_t wc = L'x';
    std::string esc_s = "Hello\tWorld\n\x41\102";
    bool esc_ok = b_true && !b_false && (wc == L'x') &&
                  (esc_s == "Hello\tWorld\nAB") && ('\\' == 92) && ('\'' == 39) && ('\"' == 34);
    cases.push_back({
        "bool_char_and_escape_sequences",
        lab::jbool(true),
        lab::jbool(esc_ok),
        esc_ok
    });

    // Case 3: Adjacent string literal concatenation & line continuation
    std::string greeting1 = "hello, bluesky";
    std::string greeting2 = "hello, "
                            "bluesky";
    std::string greeting3 = "hello, \
bluesky";
    bool str_ok = (greeting1 == greeting2) && (greeting1 == greeting3);
    cases.push_back({
        "string_literal_concatenation",
        lab::esc("hello, bluesky"),
        lab::esc(greeting2),
        str_ok
    });

    // Case 4: #define vs const vs constexpr
    const int LENGTH = 10;
    const int WIDTH = 5;
    constexpr int AREA_CE = LENGTH * WIDTH;
    int area_macro = LENGTH_MACRO * WIDTH_MACRO;
    bool const_ok = (AREA_CE == 50) && (area_macro == 50) && (NEWLINE_MACRO == '\n');
    cases.push_back({
        "define_const_and_constexpr",
        lab::jint(50),
        lab::jint(AREA_CE),
        const_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-constants-literals-lab/explore-cpp-constants-literals.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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