So we have #include <iostream> and using namespace std. I though that's how it works: when we use functions from libraries, like cin, we should write name_of_library::function, but additionally to #include we can use namespace library_name and don't write name_of_library::function anymore.
But then I understood that std and iostream are not straightforward connected -- we don't write iostream::cin, after all. So std is a namespace for some default libraries, like iostream, fstream, vector? How do we work with non-default libraries then?
A namespace name can be whatever the library author wants it to be.
A library can also contain multiple namespaces - and/or nested namespaces.
There is also no requirement that a library uses a namespace at all.
There's absolutely no relationship between library names, file names and namespace names etc (unless the library author chooses to create/maintain such a relation).
Related
I'm reading codes that designed an OCR model for chemistry. And I see some lines like this
#include <math.h> // fabs(double)
Is it possible to import just one function like what Python does (from module import function)?
I read the code of cmath. When I do #include <cmath>, the inner codes already do #include <math.h> , thus importing all the functions to the global namespace. What should I do?
With most C++ standard library headers, if you don't do using namespace std; they do not pollute the global namespace and put everything into the namespace std instead. So as long as you don't do using namespace std; there will be no pollution.
<math.h>
is a bit different in that regard. It is one of the standard library headers inherited by C++ from C and therefore it doesn't follow the namespace rules of the C++ standard library. C doesn't have namespaces at all and so including the header will put all its names in the global namespace. There is no way to avoid this. Selective inclusion from headers is not possible in C and C++.
There is an alternative form of the same header:
#include <cmath>
This header will put all the standard library functions into the std namespace as for typical C++ standard library headers.
Unfortunately, the C++ standard does allow compilers/standard library implementations to also put the names in the global namespace when <cmath> is included. So even if you use this variant, you have no guarantee that it won't pollute the global namespace and I think indeed most implementations do.
Unfortunately you have to live with C standard library functions which were inherited into C++ polluting the global namespace. I don't think there is anything that can be done about it.
What you can do is put all of your own program's declarations into a namespace, e.g.:
namespace my_stuff {
// ...
}
If there is a name conflict, then in most situations your own one will be chosen over the global one as long as you are staying in the namespace.
From what I understand, the namespace std contains all of C++ standard libraries, and one of the standard libraries is the iostream and it has the objects cout and cin.
std namespace
|
iostream
|
cout, cin
Is the above structure correct? Or is it different?
From what I understand, the namespace std contains all of C++ standard
libraries, and one of the standard libraries is the iostream and it
has the functions cout and cin.
Not quite.
Although almost all of the standard library is in namespace std, we also have the C components outside of std when using the old header form (<stdio.h> instead of <cstdio>), and the assert macro, which is not scoped.
std::cin and std::cout are not functions but objects.
<iostream> is just the name of a header which contains parts of what the C++ ISO standard officially names "Input/output library" (std::iostream is a rarely used typedef for the std::basic_iostream<char> class).
Certain components of the library can be pulled in by different #includes. For example, std::initializer_list becomes available through <initializer_list> or through includes like <vector>.
Without going into too much detail, header files and scoping are two orthogonal concepts in C++. In other words, they exist in parallel to each other. There is no useful hierarchy among/between them.
You are correct when you assume that everything that's in the standard library is contained in the std namespace, with a few exceptions, as per 17.6.1.1/ยง3 in the standard:
All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std. It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in an inline namespace inside that namespace.
Then, when you say "the libraries", there's only one library here, the "standard library". iostream is a header of this library. But that doesn't mean everything from a header is in a specific namespace.
For example, cin and cout are directly in the std namespace, but are included with the <iostream> header.
My program originally used using namespace std; but I have heard from numerous people that this is bad programming practice so I want to break the habit. I changed all of the couts and endls in my program to std::cout and std::endl;.
Aside from cout and endl, what uses the namespace std? For example: I have vectors and strings in my program; do they use std::?
A list would be nice if it's not too long or too much of a hassle.
For example: I have vectors and strings in my program; do they use std::?
Yes. Vectors and strings are part of the standard library. The members of the std namespace are in the standard library.
A list would be nice if it's not too long or too much of a hassle.
I can't give you a long, concise list. But I can show you how to find the members of the standard library. For example, take your other question. Are strings and vectors part of the standard library?
http://en.cppreference.com/w/
If we go here, click on 'Strings library' we can infact see the different types of strings we can employ in our C++ program. This process is the same for vectors, under the 'Containers library' section we can see vector, list, map etc.
If you have an IDE, you can also view the members of the standard library by prefacing your statement with std:: -
Although you do have to have a matching header file included to see members of the standard library, the headers are named as you'd expect,
#include <list>
#include <vector>
#include <string>
Dumping the entire std namespace into your program is perhaps not the wisest choice, but I wouldn't be troubled by using specific elements from std. So instead of writing using namespace std you can use using std::cout, using std::endl, etc. Having to qualify lots and lots of code does not, in my opinion, help readability.
As for what's in std, it's a very long list, including all of STL. Just try to compile and see where the compiler complains.
In short:
For everything in the C++ standard library.
That includes datastructures, algorithms, input output functions, type_traits, string manipulation, almost everything you can find in the C standard library... . You can find a list e.g. at cppreference.com
Almost all types and functions provided by the C++ standard library are in namespace std. Roughly speaking, this includes everything in standard headers without a .h in their filename - like <vector>, <iostream> (which brings in std::cout), <string> (std::string), <algorithm> (standard algorithms), and EVERY other standard C++ header file. It does not include the C standard library (<stdio.h>, <stdlib.h>, etc) although these do have equivalents in the C++ standard library (<cstdio>, <cstdlib>, etc) which do place their declarations into namespace std.
There is no point in writing a list. If you are using any part of the C++ standard library, you will be working with namespace std. So any reference of the C++ standard library is the defacto list.
There are a couple of exceptions (e.g. names in the standard headers that are specified to be macros) but those are rare.
I'm writing a library to use in my projects. In it, I was hoping to wrap the c standard library in my library's namespace and a cstd namespace to avoid having its functions in the global namespace. However, from a previous question I asked and from what I've tested, I can't just #include everything in a namespace. Is there any way to do this?
I doubt it, unless you wanted to rewrite everything.
The C language itself has no concept of namespaces, so everything the C standard library uses must rely on the fact that whatever it is looking for resides in the global namespace.
If you simply wrapped a namespace around your #includes, the compiler wouldn't be able to find anything because it wouldn't know what namespace to look in.
The usual approach would be to put the 3rd party includes in the implementation files to keep them from polluting your api.
myapi.hpp
void coolthing( int howcool );
myapi.cpp
#include <coollib.h>
void coolthing( int howcool )
{
coollib_coolthing( howcool );
}
Does the runtime library pollute the global namespace?
I don't think you really understand the terms you are using, but the names in the C++ Standard Library exist in the std namespace, so the answer is no. The names in the header files that originated in the C Standard Library are placed in the global namespace, if you #include them via the .h filenames, such as:
#include <stdio.h>
if you #include them like this:
#include <cstdio>
then the names exist in both global and std namespaces.
The reason for placing objects in the std namespace is not so much to avoid "namespace polution", which is hardly a value neutral term, but to avoid name clashes.
The runtime library is required to use reserved identifiers. Without namespace qualification, these must begin with two underscores: __start, etc.
You are not allowed to use reserved names. The library is not allowed to use your names. If either crosses over to the other, it is "pollution" which is illegal.
Essentially, the situation is the same for C and C++ except, as the other answers point out, in C++ most standard library names have namespace qualification.
Most of the library cleanly goes in namespace std: -- however, for backwards compatibility with C, you may choose to "pollute" the global namespace by including old headers like <math.h> (which puts lots of names in the global namespace) instead of the proper new ones like <cmath> (which uses the std: namespace properly).