败犬日报 2024-11-21
1. 函数里移动一个左值引用传入的参数是否合理
cpp
void func(A &a) {
funcB(std::move(a));
}
没问题的,注释要写清楚。
2. unique_ptr
自定义删除器中是不是没有必要判断指针是否为 nullptr
没有必要。
3. 面试题,求最大长度为 k 的最大子段和
线性复杂度的做法是,在前缀和上跑单调队列求长度等于 k 的区间最小值,用右端点减去这个最小值。
4. 独立的两个类 A B,A a; B* p = (B*)&a;
是不是 UB
不是 UB,解引用 p 才是 UB。
5. 给 lambda 加上 always inline
如果是 GCC/Clang:
cpp
int x = 1;
__attribute__((always_inline)) inline void foo(int y) {
x = y;
}
int main() {
auto bar = [] __attribute__((always_inline)) (int y) {
x = y;
};
foo(2);
bar(3);
}
一般可以用这个宏定义:
cpp
#if defined(__GNUC__) || defined(__clang__)
// GCC or Clang Compiler
#define ALWAYS_INLINE __attribute__((always_inline)) inline
#define ALWAYS_INLINE_LAMBDA __attribute__((always_inline))
#endif
6. 有什么不能 always_inline 的情形
递归,函数指针。
7. boost::json 性能和 rapidjson 接近,部分 payload 还能暴打
https://230.jsondocs.prtest.cppalliance.org/libs/json/doc/html/json/benchmarks.html
性能更好的考虑 simdjson。
8. 不同 json 解析库行为可能不一致
一些 corner case,比如转义字符不一致,nan 处理等。