Functions with class arguments are leaked from a namespace?

前端 未结 4 1212
礼貌的吻别
礼貌的吻别 2021-02-02 11:14

I have a small piece of code here for your consideration which puzzles me quite a lot. The strange thing is that it compiles on both Sun Studio and GCC even though I think it sh

4条回答
  •  伪装坚强ぢ
    2021-02-02 11:55

    It's due to "argument dependent lookup". Removing the const will not change the behavior you're seeing. To demonstrate that it's ADL, try moving the St struct outside of the namespace...

    struct St
    {
       int a;
    };
    
    namespace name
    {
      void f(const St& st);
      void g(int a);
    }
    
    int main(int argc, char** argv)
    {
      St st;
    
      name::f(st); 
      f(st);  // <--- now you will get the expected compile error
    
      name::g(42);
      // g(42);  <--- this does not, as I expected
    }
    

提交回复
热议问题