C ++ Primer study note _ 94 _ tools for large programs

Source: Internet
Author: User

Tool for large programs-namespace [continued 3]



Vi. overload and namespace

As we can see, each namespace maintains its own scope. Therefore,Two different namespacesOfMember functions cannotMutualHeavy Load. However, a given namespace can contain a group of overload function members.



1. Candidate functions and namespaces

Namespaces have two influences on function matching. One effect is obvious:UsingDeclare orUsingIndicates that a function can be added to a candidate set.. The other effect is much more subtle.

As you can see in the previous section, there are one or moreClass type parametersThe function name search includes defining the namespace of each parameter type. This rule alsoHow to determine the candidate set of impactTo find a candidate function and find each namespace that defines the parameter class (and defines its base class ).Add a candidate set.Even if these functions are not visible at the call point, they are added to the candidate set:

namespace NS{class Item_base{    //...};void display(const Item_base &){    //...}}class Bulk_item :public NS::Item_base{    //...};int main(){    Bulk_item book;    display(book);  //OK}

Explanation: The candidate function called by display is not only a visible function declared in the place where the display function is called, but also a function declared in the namespace of the Bulk_item class and its base class Item_base. The function display (constItem_base &) declared in the namespace NS is added to the candidate function set.



2. Heavy Load and using Declaration

Using declares a name. There is no way to write using declarations to reference specific function declarations:

    using NS::print(int);//Error    using NS::print;//OK

If the internal functions of the namespace are overloaded, the using Declaration of the function nameDeclared all functions with this name.

This is a function introduced by the using declaration. The declaration of any other functions with the same name in the scope of the using declaration is reloaded.

If the using declaration introduces a function in the scope of a function with the same name and with the same parameters, the using Declaration fails. Otherwise, the using defines another re-load instance with the given name, the effect is to increase the set of candidate functions.



3. Heavy Load and using instructions

Using indicates that the namespace member is promoted to the peripheral scope. If the namespace function and the function in the namespace's scope areSame nameTo load the namespace memberReload collectionMedium:

namespace libs_R_us{extern void print(int);extern void print(double);}void print(const std::string &);using namespace libs_R_us;void fooBar(int val){    print("Value: ");    print(val);}

4. Overload across multiple using indicators

If there are many using indications, the names from each namespace become components of the candidate set:

namespace AW{int print(int);}namespace Primer{double print(double);}using namespace AW;using namespace Primer;long double print(long double);int main(){    print(1);//AW::print(int)    print(3.1);//Primer::print(double)}

The overload set of the print function in the global scope contains the print (int), print (double), and print (longdouble) functions, even if these functionsOriginally declared in different namespacesThey are all components of the reload set for function calls in main.

// P614 exercise 17.22 // 1 namespace primerLib {void compute (); void compute (const void *);} using primerLib: compute; void compute (int) {cout <"int" <endl;} void compute (double, double = 3.14); void compute (char *, char * = 0); int main () {compute (0); // compute (int )}

//2namespace primerLib{void compute();void compute(const void *){    cout << "const void *" << endl;}}void compute(int);void compute(double,double = 3.14);void compute(char *,char * = 0);int main(){    using primerLib::compute;    compute(0); //compute(const void *)}

VII. namespaces and templates

The template declaration in the namespace affectsHow to declare template Specialization: The explicit features of a template must be declared in the namespace that defines the general template. Otherwise, the features will be different from the Specific templates.

There are two methods to define the features: one isReopen the namespace and add the special definitionThis can be done because the namespace definition is not consecutive; Or, you can useDefine features in the same way as defining external namespace members in a namespace: Use the namespace nameCustomized Template Name Definition.

[Careful mine]

To enable the customization of the template defined in the namespace, you must ensure that the customization is made in the namespace containing the original template definition.

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.