CppCoreGuidelines C.162 重载大致相似的操作符
16 January 2023
“Overload operations that are roughly equivalent”
理由
逻辑上相似的操作函数,使用不同的参数类型,却用了不同的名字,容易让人费解。导致在函数名上编码参数类型,无法实现泛型编程。
例子
考虑:
void print(int a); void print(int a, int base); void print(const string&);
这几个函数的作用都是打印出提供给它的参数。
相反的:
void print_int(int a); void print_based(int a, int base); void print_string(const string&);
这几个函数也是打印出参数,但是在函数名上硬编码了参数类型,就无法支持泛型代码。