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

验证命名空间 :: 消歧、块级 using 指令/声明、分块扩展及经典/C++17 嵌套命名空间

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-namespaces.cpp
explore-cpp-namespaces.cpp85 行
#include <iostream>
#include <string>

namespace first_space {
    std::string func() { return "Inside first_space"; }
    std::string tag() { return "F1"; }
}

namespace second_space {
    std::string func() { return "Inside second_space"; }
    std::string tag() { return "S2"; }
}

// Discontiguous namespace split across two blocks
namespace app_ns {
    int part_a() { return 15; }
}

namespace app_ns {
    int part_b() { return 27; }
}

// Classic nested namespace and C++17 compact nested syntax
namespace outer_space {
    std::string who() { return "outer"; }
    namespace inner_space {
        std::string who() { return "inner"; }
    }
}

namespace outer_space::v2 {
    std::string who() { return "cpp17_nested"; }
}

int main() {
    // 1. Scope resolution operator :: disambiguating identical names
    std::string act1 = first_space::func() + " | " + second_space::func();
    const std::string exp1 = "Inside first_space | Inside second_space";
    bool ok1 = (act1 == exp1);

    // 2. Scoped using directive & selective using declaration
    std::string via_dir;
    {
        using namespace first_space;
        via_dir = func();
    }
    std::string via_decl;
    {
        using second_space::tag;
        via_decl = tag() + ":" + first_space::tag();
    }
    std::string act2 = via_dir + "|" + via_decl;
    const std::string exp2 = "Inside first_space|S2:F1";
    bool ok2 = (act2 == exp2);

    // 3. Discontiguous namespace merging members from separate blocks
    int sum = app_ns::part_a() + app_ns::part_b();
    bool ok3 = (sum == 42);

    // 4. Nested namespaces (classic & C++17 A::B) and using namespace A::B
    std::string nested_call;
    {
        using namespace outer_space::inner_space;
        nested_call = who();
    }
    std::string act4 = outer_space::who() + "->" + nested_call + "->" + outer_space::v2::who();
    const std::string exp4 = "outer->inner->cpp17_nested";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"namespace_scope_resolution\", \"expected\": \"" << exp1
              << "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"using_directive_and_declaration\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"discontiguous_namespace_merge\", \"expected\": 42, \"actual\": "
              << sum << ", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"nested_and_cpp17_namespaces\", \"expected\": \"" << exp4
              << "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    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-namespaces-lab/explore-cpp-namespaces.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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