C ++ templates Study Notes (10) -- name in chapter 9 Template

Source: Internet
Author: User

1 Chapter 9 Template Name

C ++It is a context-related language: that is, we cannot break away from its context structure.

 

1.1 Name category

LRestricted name. If there is a domain resolution operator before::, Or the member access operator (.Or->). For exampleThis-> count;

LIf a name depends on a template parameter, it is called the dependency name. For exampleSTD: vector <t >:: iterator, IfTIs a template parameter, It is the dependency name, ifTIsTypedefIs not the dependency name.

 

1.2 Search by name

Common search: In the member function definition defined within a class, it first looks for the scope of the class and the base class before finding the peripheral scope. This query method is not restricted.

However, you should also add a parameter-dependent query (that isADL).

 

1.3 ADL = argument-depentent Lookup

In a function call, if the name is followed by a real parameter expression in the angle bracketsADLTheAssociated classAndAssociated namespace.

ADLIt can only be applied to unrestricted function names.This unrestricted name indicates that the function name is not restricted..

If this name can be found in common searches, it is not used.ADL.

Let's take a look at the following example:

 

// 9.2

Template<Typename T>

Inline T Const&Max(T Const&A,T Const&B)

{

Return A<B?B:A;

}

 

Namespace Bigmath

{

Class Bignumber{

Public:

Bignumber(Int I):Mem(I){};

Private:

Int Mem;

 

Public:

Int Getmem()Const{

Return Mem;

};

};

 

Bool Operator <(Bignumber Const&A,Bignumber Const&B)

{

Return A.Getmem() <B.Getmem();

}

}

 

 

Int _ Tmain(Int Argc,_ Tchar*Argv[])

{

// 9.2

Bigmath::Bignumber Bignum1(2 ),Bignum2(3 );

STD::Cout<"Max Nums is :"<Max(Bignum1,Bignum2).Getmem() <STD::Endl;

Return0;

}

In the above example, normally, we should not seeBinmathOperators in a namespaceOperator <Unless there is a special rule, this special rule isADL.

 

Let's take a look at the following example:

Namespace X{

Template<Typename T>Void F(T)

{

STD::Cout<"Int template <typename T> void F (t )"<STD::Endl;

}

}

 

Namespace N

{

Using Namespace X;

Enum E{E1};

Class Tryusetemplatef{};

Void F(E){

STD::Cout<"Void F (e) in namespace N"<STD::Endl;

};

}

 

Void F(Int I)

{

STD::Cout<"Void F (INT) I ="<I<STD::Endl;

}

Int _ Tmain(Int Argc,_ Tchar*Argv[])

{

// 9.2.1

// 9.2.1

::F(N::E1);// 1

F(N::E1);// 2

N::Tryusetemplatef Aobj;

F(Aobj);// 3

Return0;

}

The compiler compiles3An error is reported. It indicates that the corresponding function overload cannot be found.

As you can seeADLNamespaceNInUsing DirectiveWill be ignored. Because if it is not ignored, it can find the namespaceXTemplate FunctionsFInstead of compiling errors.

 

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.