Argument dependent lookup [Article]----C + + must know

Source: Internet
Author: User

Namespaces have far-reaching implications for modern C + + programming and design. Some of these effects are straightforward and obvious, such as using declarations and using directives, and names that are qualified by namespace scopes. However, namespaces have some very basic and important effects that are less grammatical. An argument-dependent lookup (Argument Dependent lookup ADL) is one of them. Like many C + + features, ADL can become complex in some situations, but in general it is fairly straightforward to use and solves more problems than it introduces.

The idea behind ADL is very simple. When looking for a function name in a function call expression, the compiler is also checked in the namespace containing the type of the function call argument. As an example, consider the following code:

#include <iostream>namespaceorg_semantics{classx{}; voidFConstX &){}    voidG (X *) {} Xoperator+(Constx& A,Constx&b) {     returnA//added for normal compilation    }    classstring{}; Std::ostreamoperator<< (Std::ostream &a,ConstString &b) {        returnA//added for normal compilation    }};intG (org_semantics::x*a) {    return 0;//added for normal compilation}voidAfunc () {org_semantics::x A; f (a);//call Org_semantics::f argument dependent lookup//I use VC implementation of the effect is reversed, here error, undefinedg (&a);//Error! has ambiguous nature//error C2668: ' G ' ambiguous call to overloaded function //I use VC to achieve the effect is reversed, compiled here through A=a+a;//Call org_semantics::operator+}intMain () {return 0;}

Normal find does not find

Argument dependent lookup [clause]----C + + must know required

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.