cpp-best-practices / cpp-best-practices/cppbestpractices
Const as Much as Possible (Not!)
- 主要語言
- 沒有語言資料
- 星號
- 8.8k
- 分支
- 902
- PR 合併指標
- 30 天內沒有已合併 PR
描述
While "Const as Much as Possible" is generally good practice, `const` should not be directly used on function declaration parameters, including member functions.
A quick example:
```
void foo (int x);
void foo (int const x) {return x * 2;}
void bar (char const * s);
void bar (char const * const s) {puts(s);}
```
Notice I said "directly". Hence `s` isn't `const` in the declaration, but is in the definition (direct), but what `s` points at is `const` in both, and must match in both (indirect).
The reason for doing this is to not leak a function's implementation details. Because whether a function treats a parameter variable as `const` or not is an implementation detail of the function. And if a function gets modified such that its parameter is no longer treated as `const`, then callers of that function shouldn't have to be recompiled.
This is a difference between C and C++. In C, the function definition's parameter type(s) must exactly match that of the function declarations. (At least it used to. I haven't actually developed anything significant with modern C.)
貢獻指南
這個儲存庫沒有索引到貢獻指南
研究方向
issue 中沒有指定檔案或測試。首先定位涵蓋 const 函式參數的 best practices 部分,然後將其中的指引與報告中的 C 和 C++ 範例進行比較;當文件明確區分參數本身的直接 constness 與所指向資料的 constness 時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- cpp
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100