Static, nonmember or static nonmember function?

前端 未结 3 638
轮回少年
轮回少年 2021-02-06 00:30

Every time I have some functionality which is in the direction of \"utility\", I end up wondering which option is the best. For instance, printing message structs (own or extern

3条回答
  •  情话喂你
    2021-02-06 01:17

    I use struct with static functions, because it offers a slighly better isolation than non-member functions in namespaces (due to the avoidance of Koenig lookup).

    To give an example of the thing I want to avoid:

    namespace Foo
    {
          struct A
          {
          };
    
          void f(const A& a) {}
    }
    
    void f(const Foo:A& a) { std::cout << "AAA"; }
    
    int main(void)
    {
          Foo::A a;
          f(a); // calls Foo:f
          return 0;
    }
    

提交回复
热议问题