27.7 — 函数 try 块

Try 和 catch 块在大多数情况下都足够好用,但在一种特定情况下它们不够用。考虑以下示例: #include <iostream> class A { private: int m_x; public: A(int x) : m_x{x} { if (x <= 0) throw 1; // 异常在这里抛出 …

15.3 — 嵌套类型(成员类型)

考虑以下这个小程序: #include <iostream> enum class FruitType { apple, banana, cherry }; class Fruit { private: FruitType m_type { }; int m_percentageEaten { 0 }; public: Fruit(FruitType type) : m_type { type } { } FruitType getType() { return m_type; } int getPercentageEaten() { return m_percentageEaten; } …

26.3 — 函数模板特化

当为给定类型实例化函数模板时,编译器会根据模板函数创建一个副本,并用变量声明中使用的实际类型替换模板类型参数。这意味着特定函数对于每个实例化类型都将具有相同的实现细节(只是…