C + + Namespace detailed

Source: Internet
Author: User

The namespace is defined in the following format: (taken from the C + + standard documentation)

named-namespace-definition:                   namespacenamespace-body}unnamed- namespace-definition:                   namespacenamespace-body}  Namespace-body:                   declaration-seqopt
Well-known namespaces:       namespace namespace name {                         Declaration sequence optional       }  unnamed namespaces:       namespace {                         declaration sequence optional       }   The so-called namespace in C + + refers to the various visible ranges of identifiers. All identifiers in the C + + standard library are defined in a namespace called Std. First, <iostream> and <iostream.h> <iostream> and <iostream.h> are not the same, the former has no suffix, in fact, in your compiler include folder can be seen  , the two are two files, open the file will find that the code inside is not the same. The header file with the suffix. h C + + standard has been explicitly not supported, the earlier implementation of the standard library functions defined in the global space, declared in the header file with the. h suffix, C + + standard in order to distinguish with C, and in order to correctly use the namespace, the header file does not use the suffix. h.                             So, when using <iostream.h&gt , the equivalent of calling the library function in C, using the global namespace, which is the earlier C + + implementation; When using <iostream>, the header file does not have a global namespace defined and must use namespace Std to properly use cout.   Second, the so-called namespace, refers to the various visible range of identifiers. All identifiers in the   C + + standard library are defined in a namespace named Std.   Due to NamEspace concept, when using any identifier of the C + + standard library, there can be three choices:  1, directly specifying identifiers. For example Std::ostream rather than ostream. The complete statement is as follows: Std::cout << std::hex << 3.4 << std::endl;  2, use the Using keyword. Using Std::cout; Using Std::endl; The above program can be written as cout << Std::hex << 3.4 << endl;  3, the most convenient is to use using namespace std;  such as: 
#include <iostream><sstream><stringusingnamespace  //3.4
Because the standard library is very large, it is very likely that the programmer will have the same name or name of the class in the standard library as it is chosen. So in order to avoid the name conflict caused by this situation, everything in the standard library is placed in the namespace Std. But this poses a new problem. Countless original C + + code relies on functionality in pseudo-standard libraries that have been used for years, all in the global space. So there are <iostream.h> and <iostream> and so on such a header file, one is to be compatible with the previous C + + code, one is to support the new standards. The namespace std encapsulates the name of the standard library, and the standard library is generally not ". h" in order to distinguish it from previous header files.today, using Visual C + + to write a small program (VS2005), very simple, very simple, but it is not compiled by a strange problem: Error 1 C2668: "Max": the call to overloaded functions is ambiguous

The initial code is as follows:

#include      using namespace   std; Template   t max (T a,t b) {      return (a>b)?    A:B);   }  void  main () {     double  x, y;       cin>>x>>y;    cout<<""<< (max (x, y) <<endl;       cin  >>x; }
I put this piece of code into VC + + 6.0 unexpectedly passed, the program is also normal operation. It makes me baffled. Then finally figured it out!In fact, there is a max function under the Std namespace, and the same function is implemented ... I fainted. Use the Go To Definition feature to see how Microsoft writes the Max function. In order not to be despised in this case, the Microsoft code is not posted. Understand why this error occurs, we rewrite the code as follows:
  #include  using   Std::cin;    using   Std::cout;    using   Std::endl; Template T max (T a,t b) { return  ((a>b)? A:B);  void   main () { double   x, y;       CIN  >>x>>y; cout  << max number is   " << (max (x, y) <<endl;    cin  >>X; } 
This is my recommended practice, because C + + PRIMER, effective C + + is used in this way, but rectification's book is a using namespace std; I think it's very concise, I have been using it, did not expect to bring so many problems,     In the past, the friend function also encountered inexplicable errors. In fact, there are two simple solutions, that is, to change their definition of the function to other names, or directly with the functions provided by Microsoft. I believe that Microsoft will not be able to provide more efficient than we write low ~ Well, it is written here. Hope that we develop good programming habits, ^-^ Moreover, since we have been directly using the following Microsoft's own library files:
" iostream " using namespace std;

Link here for Baidu Encyclopedia, for the detailed description of the namespace: http://baike.baidu.com/link?url=epiFYb_ Hsw0nx6sig9eqefxthpr1w2suvnfazodscueqt1g-zcjxvkz8pnx8zysgrxfmsdtpqs1dybtemlysgk

In fact, in other words, I understand that the namespace STD is special, because STD is a standard library that Microsoft has defined, and if you do not use the "using namespace Std", you cannot invoke any function in the STD library. Look at the following code:
namespace test{     <class t>     class  allocator     {      public  :               .........     }}
Put a declarative template in the namespace test, if you are using the "using namespace test" to use the template declared in the namespace, this will be an error, "Error C3861:" Class Allocator ": Cannot find the identifier ", in fact, it can be understood that the namespace is the definition of space in a box, and the namespace name is its domain, you just call the" using namespace namespace name ", equivalent to the domain into the action scope, and then use it will not be wrong. I have written a very simple test example:
//Display.h declares a display function with a namespace#pragmaOnce#include"iostream"using namespacestd;namespacetest{voidDisplay () {cout<<"Hello World!!!"<<Endl; }} //Test.cpp functions declared within the namespace of the main function call#include"stdafx.h"#include"Display.h"using namespaceTest//This namespace is called, and the display function declared in the namespace can be used directly below//If the sentence is masked, the compiler prompts the error "Error C3861:" Display ": identifier not found"int_tmain (intARGC, _tchar*argv[])      {Display (); return 0;} 

C + + Namespace detailed

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.