cpp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Critical Patterns

关键模式

Smart Pointers (REQUIRED)

智能指针(必填)

cpp
// ✅ ALWAYS: Use smart pointers
auto user = std::make_unique<User>("John");
auto shared = std::make_shared<Config>();

// ❌ NEVER: Raw new/delete
User* user = new User("John");
delete user;
cpp
// ✅ 务必使用:智能指针
auto user = std::make_unique<User>("John");
auto shared = std::make_shared<Config>();

// ❌ 禁止使用:原始new/delete
User* user = new User("John");
delete user;

RAII (REQUIRED)

RAII(必填)

cpp
// ✅ ALWAYS: Resource management through constructors/destructors
class FileHandle {
    std::fstream file_;
public:
    FileHandle(const std::string& path) : file_(path) {}
    ~FileHandle() { if (file_.is_open()) file_.close(); }
};
cpp
// ✅ 务必使用:通过构造函数/析构函数管理资源
class FileHandle {
    std::fstream file_;
public:
    FileHandle(const std::string& path) : file_(path) {}
    ~FileHandle() { if (file_.is_open()) file_.close(); }
};

Modern Patterns (REQUIRED)

现代模式(必填)

cpp
// ✅ Use auto for type deduction
auto items = std::vector<int>{1, 2, 3};

// ✅ Use range-based for loops
for (const auto& item : items) {
    process(item);
}

// ✅ Use structured bindings
auto [name, age] = getPerson();

cpp
// ✅ 使用auto进行类型推导
auto items = std::vector<int>{1, 2, 3};

// ✅ 使用基于范围的for循环
for (const auto& item : items) {
    process(item);
}

// ✅ 使用结构化绑定
auto [name, age] = getPerson();

Decision Tree

决策树

Need unique ownership?     → std::unique_ptr
Need shared ownership?     → std::shared_ptr
Need optional value?       → std::optional
Need multiple types?       → std::variant
Need string views?         → std::string_view

需要唯一所有权?     → std::unique_ptr
需要共享所有权?     → std::shared_ptr
需要可选值?         → std::optional
需要多类型支持?     → std::variant
需要字符串视图?     → std::string_view

Commands

命令

bash
g++ -std=c++20 -Wall -Wextra main.cpp -o app
clang++ -std=c++20 main.cpp -o app
cmake -B build && cmake --build build
bash
g++ -std=c++20 -Wall -Wextra main.cpp -o app
clang++ -std=c++20 main.cpp -o app
cmake -B build && cmake --build build