28 July 2022

C++ 核心指南目录

“Place helper functions in the same namespace as the class they support”

理由

帮助函数(Helper Function)不直接访问类的内部数据,但也是类的有用的接口。帮助函数和类放在一个名字空间里,可以强化他们的关系,查找函数的参数类型也更方便些。

例子

namespace Chrono { // here we keep time-related services

    class Time { /* ... */ };
    class Date { /* ... */ };

    // helper functions:
    bool operator==(Date, Date);
    Date next_weekday(Date);
    // ...
}

注意

此规则尤其适用于重载的操作符函数。

强化

标注参数都是一个名字空间的全局函数。