C++核心準(zhǔn)則C.162:將大致相同的操作設(shè)計(jì)為重載函數(shù)?

C.162: Overload operations that are roughly equivalent
C.162:將大致相同的操作設(shè)計(jì)為重載函數(shù)
Reason(原因)
Having different names for logically equivalent operations on different argument types is confusing, leads to encoding type information in function names, and inhibits generic programming.
邏輯上等價(jià),只是參數(shù)不同的操作具有不同的名稱是難以理解的,會(huì)導(dǎo)致函數(shù)名中包含類型信息,而且阻礙編程共通化。
Example(示例)
Consider(考慮下面的代碼):
void print(int a);
void print(int a, int base);
void print(const string&);
These three functions all print their arguments (appropriately). Conversely:
這三個(gè)函數(shù)都(恰當(dāng)?shù)?打印參數(shù)指定的內(nèi)容。相反地,
void print_int(int a);
void print_based(int a, int base);
void print_string(const string&);
These three functions all print their arguments (appropriately). Adding to the name just introduced verbosity and inhibits generic code.
這三個(gè)函數(shù)都(恰當(dāng)?shù)?打印參數(shù)指定的內(nèi)容。只是在函數(shù)名中增加了冗長的,阻礙共通化編程的信息。
Enforcement(實(shí)施建議)
???
原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c162-overload-operations-that-are-roughly-equivalent
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
