Monday, May 24, 2010

In C++, what exactly is namesapce std, in relation to header files, like iostream.?

I know "using namespace std;" is necessary for using cout, cin. And if you did not use "using namespace std;" you would have to write for example std::cout, etc.





But is "using namespace std;" just for use with iostream, or does it apply to ALL of the functions in ALL the libraries?





So, even if I'm not using iostream, do I have to use "using namespace std;" if I don't want to start every function name with "std::function"?





Thanks guys.

In C++, what exactly is namesapce std, in relation to header files, like iostream.?
%26gt;%26gt; But is "using namespace std;" just for use with iostream, or does it apply to ALL of the functions in ALL the libraries?





std is one of the namespaces in the io header file.





A "namespace" is a grouping of classes that are all related to performing a specific task. In the case of std, that namespace applies to the "standard" input / output (I /O) devices -- namely, the keyboard and the screen. All the classes in the IO header that apply to sending data to the screen or accepting it from the keyboard -- including cin and cout -- are contained in the std namespace.





%26gt;%26gt; So, even if I'm not using iostream, do I have to use "using namespace std;" if I don't want to start every function name with "std::function"?





If you don't intend to accept keyboard input or send output to the screen, no, you do not need to include the IO header nor call the std namespace.





http://www.dougv.com/blog/2006/12/22/obj...
Reply:Using C++ Library Headers





You include the contents of a standard header by naming it in an include directive, as in:





#include %26lt;iostream%26gt; /* include I/O facilities */





You can include the standard headers in any order, a standard header more than once, or two or more standard headers that define the same macro or the same type.





A C++ library header includes any other C++ library headers it needs to define needed types. (Always include explicitly any C++ library headers needed in a translation unit—for proper compilation, to avoid your guessing wrong about its actual dependencies.) In contrast, a Standard C header never includes another standard header. A standard header declares or defines only the entities described for it in this document.





Every function in the library is declared in a standard header. Unlike in Standard C, the standard header never provides a masking macro, with the same name as the function, that masks the function declaration and achieves the same effect.


____________________





All names other than operator delete and operator new in the C++ library headers are defined in the std namespace, or in a namespace nested within the std namespace. You refer to the name cin, for example, as std::cin.





Note, however, that macro names are not subject to namespace qualification, so you always write __STD_COMPLEX without a namespace qualifier. In some translation environments, including a C++ library header may hoist external names declared in the std namespace into the global namespace as well, with individual using declarations for each of the names. Otherwise, the header does not introduce any library names into the current namespace.





The C++ Standard requires that the C Standard headers declare all external names in namespace std, then import them into the global namespace with individual "Using" declarations for each of the names. But in some translation environments the C Standard headers include no namespace declarations, declaring all names directly in the global namespace. Thus, the most portable way to deal with namespaces is to follow two rules:





1.) To assuredly declare in namespace std an external name that is traditionally declared in %26lt;stdlib.h%26gt;, for example, include the header %26lt;cstdlib%26gt;. Know that the name might also be declared in the global namespace.


2) To assuredly declare in the global namespace an external name declared in %26lt;stdlib.h%26gt;, include the header %26lt;stdlib.h%26gt; directly. Know that the name might also be declared in namespace std.





Thus, if you want to call std::abort() to cause abnormal termination, you should include %26lt;cstdlib%26gt;. And if you want to call abort(), you should include %26lt;stdlib.h%26gt;.





Alternatively, you can write the declaration:





using namespace std;





which imports all library names into the current namespace. If you write this declaration immediately after all include directives, you bring the names into the global namespace. You can subsequently ignore namespace considerations in the remainder of the translation unit. You also avoid most dialect differences across different translation environments.





Unless specifically indicated otherwise, you may not define names in the std namespace, or in a namespace nested within the std namespace.





____________________

phone cards

No comments:

Post a Comment