C.163: 重載只用於基本等價的操作

C.163: Overload only for operations that are roughly equivalent

C.163: 重載只用於基本等價的操作

 

Reason(原因)

Having the same name for logically different functions is confusing and leads to errors when using generic programming.

邏輯上不同的函數使用相同的名稱會引起混淆,在使用它們進行共通化編程時容易引發錯誤。

 

Example(示例)

Consider(考慮下面的代碼):

void open_gate(Gate& g);   // remove obstacle from garage exit lane
void fopen(const char* name, const char* mode);   // open file

The two operations are fundamentally different (and unrelated) so it is good that their names differ. Conversely:

兩個操作是根本不同的(也沒有關聯),因此它們使用不同的名稱就很好。相反:

void open(Gate& g);   // remove obstacle from garage exit lane
void open(const char* name, const char* mode ="r");   // open file

The two operations are still fundamentally different (and unrelated) but the names have been reduced to their (common) minimum, opening opportunities for confusion. Fortunately, the type system will catch many such mistakes.

這還是兩個根本不同的操作(而且毫無關聯),但是名稱已經被壓縮到(共通的)最小限度,這種做法增加了混淆的可能性。幸運的是,很多這樣的錯誤都會被系統捕捉到。

 

Note(注意)

Be particularly careful about common and popular names, such as open, move, +, and ==.

特別關注那些通用和常見的名字,例如open,move,+和==等。

 

Enforcement(實施建議)

???

 

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c163-overload-only-for-operations-that-are-roughly-equivalent


 

覺得本文有幫助?歡迎點贊並分享給更多的人。

閱讀更多更新文章,請關注微信公衆號【面向對象思考】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章