Skip to content

败犬日报 2025-07-01

1. 怎么检查代码特化了标准库模板

把 clang 和 libc++ 升到 20 可以,clang 搞了个禁止特化的属性,一特化就报错。

2. Don't ask to ask, just ask(文章)

https://dontasktoask.com/

3. switch enum,每个枚举值都有 return 但是 MSVC 报 not all control paths return a value

cpp
enum class E { A, B, C };

int foo(E e) {
    switch (e) {
        case E::A:
        case E::B:
        case E::C:
            return 1;
    }
}

可以在 default(或函数末尾)里写 std::unreachable()。如果 C++23 之前可以自己写一个:

cpp
[[noreturn]]
inline void unreachable() {}

虽然这是 MSVC 独有,但是事实上标准允许 enum 的值不在定义的范围内。https://en.cppreference.com/w/cpp/language/enum.html 有记载:

Values of integer, floating-point, and enumeration types can be converted to any enumeration type by using static_cast. Note that the value after such conversion may not necessarily equal any of the named enumerators defined for the enumeration:

翻译:整型、浮点型和枚举类型的值可以通过使用 static_cast 转换为任何枚举类型。请注意,这种转换后的值不一定等于枚举定义的任何命名枚举器:

4. c++23 前怎么在 std::function 用 std::unique_ptr

std::function 要求 copyable,这种情况要么手写 move only function,要么 std::unique_ptr::release 后自己手动管理。