zoukankan      html  css  js  c++  java
  • Item 25. Argument Dependent Lookup

    Item 25. Argument Dependent Lookup

    namespaces对于C++程序和设计有很深的影响。
    它的Argument Dependent Lookup(ADL)特性非常重要,尽管潜在的增加了程序的复杂性,但它所解决的问题远比它引入的要多。

    ADL的思想很简单:当在函数调用表达式中查找函数的名字,编译器同时会检查函数参数类型所在的namespaces。eg:

    namespace org_semantics {
        class X { ... };
        void f( const X & );
        void g( X * );
        X operator +( const X &, const X & );
        class String { ... };
        std::ostream operator <<( std::ostream &, const String & );
    }
    //...
    int g( org_semantics::X * );
    void aFunc() {
        org_semantics::X a;
        f( a ); //1) call org_semantics::f, 因为a的类型为org_semantics::X,所以到     //org_semantics中查找
        g( &a ); //2) error! ambiguous...                               
        a = a + a; //3) call org_semantics::operator +
    }

    根据ADL,1)、3)的调用都没问题,2)为什么会出问题呢?
    像ADL那样复杂的规则也会让人头破血流。调用函数g时就是遇上了。在这种情况下编码器通过编译器会找到全局的g,但由于g传入的参数是org_semantics::X *, 所以又会到namespaces org_semantics下找到一个g,于是ambiguous发生了。解决办法:该其中一个的名字吧。

  • 相关阅读:
    VS2010 VC Project的default Include设置
    Linux 下的编辑/编译器
    用命令实现Win7远程桌面关机和重启
    怎样快速刪除Word中超链接?
    chrome浏览器世界之窗浏览器的收藏夹在哪?
    代码量查找工具[最好用的]
    C项目实践--网络协议和套接字编程
    memmove 和 memcopy
    bzoj2456: mode
    bzoj1205: [HNOI2005]星际贸易
  • 原文地址:https://www.cnblogs.com/aiwz/p/6333230.html
Copyright © 2011-2022 走看看