> ## Content Index
> Fetch the complete content index at: https://andybase.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 代码迷惑行为大赏
- URL: https://andybase.com/-e4-bb-a3-e7-a0-81-e8-bf-b7-e6-83-91-e8-a1-8c-e4-b8-ba-e5-a4-a7-e8-b5-8f/
- Published: 2019-11-15T17:03:29.000Z
- Updated: 2019-11-15T17:03:29.000Z
- Author: andy
- Tags: c++, 周记, #Migrated-1784083136856, #wp, #wp-post, #Import 2026-07-15 02:38

> 人无远虑，必在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 的日志
```

---