What is loaded into standard namespace? [duplicate] - c++

This question already has answers here:
cmath vs math.h (And similar c-prefixed vs .h extension headers)
(4 answers)
Closed 6 years ago.
I am currently building my first more complex C++ program that contains an own namespace. Of course, I use several imports. Ehat I found to be strange is that function included with eg.
#include <math.h>
can be accessed within the workspace
a = cos(b)
where the cos function is part of math.h. On the other hand functions included with eg.
#include <fstream>
must be accesed via
std::ifstream
or similar. I would like to understand this

C++ standard library includes most of the C library (there are some hazy details around optional parts of the C library).
Since C has no concept of namespacing, everything included from the C library will be in the global namespace. Since <math.h> is a C header, its functions are put into global namespace.
Everything included from the C++ standard library will be in the std:: namespace, like std::ifstream when you included <fstream> header.
Where it gets fun, are aliases. As an example, <math.h> can also be included as <cmath>. The idea behind this was that you select whether you want the C symbols in global namespace (including <math.h>) or in the std:: namespace (including <cmath>), but this didn't work out and generally if you include the C++ version of the header (ie <cmath>), you will get both.
Generally, if a header can either be included via <foo.h> or via <cfoo>, its a C header. C++ standard library headers do not have these aliases (except when you have to deal with non-standard things like iostream.h that apparently still exist on some platforms).

Related

Why is there a ".h" extension in <bits/stdc++.h>?

(As far as I know) C++ doesn't accept the ".h" extension on header files (as it's usually not present in other include statements) so how does the include <bits/stdc++.h> work in C++ and why does this have the ".h" extension?
why there is “.h” extension in <bits/stdc++.h>
Because the developer - who created the file - chose that name.
It is conventional to name headers with the suffix .h and the developer presumably followed such convention.
(As far as I know)c++ doesn't accept the ".h" extension header files
Your knowledge is wrong. Any "extension", including no extension are accepted. There is no limitation to how a header file can be named.
You've tagged [c++-standard-library], so I'll add that C++ standard headers are all named without a suffix, except for those inherited from the C standard library which use the conventional .h suffix (and which do have non-suffixed aliases). bits/stdc++.h is not a standard header even though it may be part of a standard library implementation.
C++ does not use the .h extension for standard library headers named in #include statements.
But bits/stdc++.h is not a standard header, so the standard library naming conventions wouldn't apply to it. It should never be used anyway.
There is no mandatory mapping, IIRC, from the name used in the include statement, to the filename. You can certainly use .h extensions on your own headers if you want, although it may get confusing if you mix C and C++ in a project.
(As far as I know)c++ doesn't accept the ".h" extension header files
That is an incorrect statement.
You can use any filename in that statement.
After the preprocessor has inserted all the files and the result is valid C++ code, then there will not be any errors during compilation.
The case with standard headers is that the C++ standard library developers have created the standard functionality in files without an extension. So the filename is "vector", so #include <vector> is used.
If the developers add a file "vector.hpp", (which has a statement #include <vector> inside the body of the file) then it has to be used as #include <vector.hpp>.

Why is every frequently used standard C function automatically included in any C++ program? [duplicate]

I use Dev C++ 5.11. TDM-GCC 4.8.1
And this code runs well.
#include<iostream>
using namespace std;
int main()
{
printf("%d\n", 42);
cout << "good";
}
But as far as I know, iostream does not include "printf". (http://en.cppreference.com/w/cpp/header/iostream)
Why this code run? iostream acutally include printf? Is it a kind of standard?
The list of header files included in a system/standard header file is library implementation dependent (that is usually associated with the compiler you're using), and (as far as I remember) the C++ standard does not prohibit one header file from automatically including another one
In your case <iostream> is probably also #including <stdio.h> (or <cstdio>).
Relying on a header file being included in another is non portable to different standard libraries, compilers and platforms, so it's better to make sure that you explicitly #include everything you need.

Are C++ versions of C standard library functions in the std:: namespace? [duplicate]

This question already has an answer here:
Why do both "std::printf" and "printf" compile when using <cstdio> rather than <stdio.h> in C++? [duplicate]
(1 answer)
Closed 7 years ago.
"C++ Primer" (5th edition) states on page 91 the advantage of including the C++ version of a C standard library header instead of the .h version: this way the included names end up in the std:: namespace and do not pollute the global namespace.
I tried including cstdio and was surprised to observe that I can use printf() without specifying std::. Interestingly, including only iostream or only string is also sufficient to get access to a global printf(). Am I missing something?
I am compiling with g++ 4.8.2 with -Wall -Wextra -Werror -std=c++11.
It is unspecified whether <cfoo> also puts the names into the global namespace. All C library names are reserved in the global namespace in C++.
[extern.names]/3:
Each name from the Standard C library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.
To restate this:
in order to use a name in std, you must include the correct header.
You must not redefine any names from the C standard library in the above contexts.
Nothing says whether any particular names from the C standard library are declared (unless you explicitly include the deprecated header <foo.h>).

What is the différence between #include <iostream.h> and #include <iostream>? [duplicate]

This question already has answers here:
Difference between iostream and iostream.h
(3 answers)
Closed 9 years ago.
What is the difference between
#include <iostream.h>
and
#include <iostream>
?
Before C++ was even standardised, the I/O library was developed as <iostream.h>. However, that header has never been a standard C++ header. Some older compilers continued to distribute the <iostream> header also as <iostream.h>. Use <iostream> because it is guaranteed by the standard to exist.
It's worth noting that the only standard headers that end with .h are the C standard library headers. All C++ standard library headers do not end with .h.
<iostream> is the usual header
<iostream.h> is the old header, not longer supported by some compilers
It just depends on the name of the file provided by your toolchain.
Some (old) compilers use .h files.
Modern compilers usually use <iostream> (without the .h extension).

#include <file> #include <file.h> - what's the difference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between <string> and <string.h>?
My specific example uses following clause:
#include <string>
If I use following clause instead
#include <string.h>
compiler ends with error
[BCC32 Error] utils.cpp(173): E2316 'getline' is not a member of 'std'
Line 173 in utils.cpp file is as follows:
while(std::getline(in, line, '\n'))
I thought that there is no difference between these two clauses. Now I am confused. What files are in fact included by these two clauses? Lets say, my C++ Builder installation has program directory C:\Program Files\RAD Studio\9.0 and include files are located in subdirectory C:\Program Files\RAD Studio\9.0\include.
#include <string>
This includes the C++ string header.
#include <string.h>
This includes the C string header, with all identifiers in the global namespace. (Deprecated.)
#include <cstring>
This includes the C string header, with all identifiers placed in the std:: namespace.
Edit: Rule of thumb - C++ headers never end on ".h". Prefix the traditional C header name with "c" and drop the ".h" to keep the global namespace clean. Use ".h" for your project's C headers only. Use ".hpp" for C++-only headers.
They are two different headers. The convention in the C standard library is to have the headers ending with .h, whereas in the C++ standard library the convention is to miss out the file extension altogether. Some more detail from wikipedia:
Each header from the C Standard Library is included in the C++
Standard Library under a different name, generated by removing the .h,
and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
The only difference between these headers and the traditional C
Standard Library headers is that where possible the functions should
be placed into the std:: namespace (although few compilers actually do
this). In ISO C, functions in the standard library are allowed to be
implemented by macros, which is not allowed by ISO C++.
Other libraries follow different conventions. Boost, for instance, chooses .hpp as their C++ header extension of choice.
By convention, C (Procedural) headers ends by '.h' "string.h", "stdio.h"... and C++ (Object oriented mostly) don't include any extension: "iostream", "string" ...
Not sure if all headers follow this convention, but I think that the standards ones all do.