人无远虑,必在on-call
纪念从业四年多,整理一个自己写过的智障代码合集
喝假酒系列
class wtf{
// bala
private:
std::string_view name;
};
// 点评:知道你学了c++17,也不能这么瞎胡闹吧
//(string_view 不拥有string 内容的指针)
void func(){
std::lock_guard<mutex> a(lock);
{
// critical section
}
// 2000 行其他逻辑
}
// 点评:很困惑,你是想锁哪部分?
//(a的声明周期“本应该”与critical section一致,但是……)
void func(ptr* a){
unique_ptr<ptr> a_ptr{a};
// bala
}
auto d = std::make_unique<ptr>();
func(d.get());
// 点评:很常见:写func的和用func的不是一个人,所以没有事先约定谁有所有权。
// 什么?是同一个人?假酒害人。
void func(){
auto d = std::make_unique<ptr>();
boost::fibers::fiber([&]() {
d->foo();
}).detach();
}
// 点评:嗯,这种不太明显的生命周期问题还真的是令人困(抓)惑(狂)
时差没倒好篇
void func(const bool & condition);
// 点评:直接copy bool 可能都比传引用要快
Ptr* func(){
Ptr object{};
return &object;
}
// 点评:你的别致很栈啊。
void func(){
for(int thread_id = 0; thread_id< MAX_THREAD; ++thread_id){
boost::fibers::fiber([&]() {
log.info("thread id is %d", thread_id);
}).detach();
}
}
// 点评:你会收获很多很多 thread id 为MAX_THREAD 的日志