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.
Related
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).
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.
Consider this bit of code:
#include <iostream>
#include <string>
int main()
{
std::string str;
std::cout << "Enter a string: \n";
getline(std::cin, str);
}
Why must I use std:: for string, cin and cout, but not getline()? Is getline() not in the standard library? I'm actually somewhat confused why I can't just write using namespace std; and not have to #include anything in the standard library, too. Thanks in advance!
Re the magic automatic namespace qualification.
It's Andrew Koenig's* fault.
He considered the problem of providing operators for user defined types. And came up with the idea of resolving the function depending on its arguments. E.g. with an argument of type defined in namespace std, the compiler looks (also) in namespace std.
That's called either Koenig lookup or ADL, short for Argument Dependent Lookup.
Re using namespace std;.
For short exploratory programs you not only can, but to save work you should, do this.
However, the standard library defines some identifiers such as distance that are very likely to collide with names you use.
Hence, for more serious code consider whether other means of avoiding verbosity, might be better. Many programmers even maintain that you should not use using namespace std; in serious code. Some Norwegian firms have this as a coding guideline (I don't agree with so absolute a view, but you should know that it's there, and may be a majority view).
At any rate:
Never put a using namespace std; in the global namespace in a header
because that makes name collisions like distance very likely for some code that includes the header.
Why using doesn't make things accessible.
Many programming languages, such as Java and C# and Ada and Modula-2 and UCSD Pascal, just to name some, have language support for separately compiled modules, where a module consists of functions, variables, types, constants, maybe more.
Depending on the language a module may be called a "module" (Modula-2), a "package" (Ada, Java), a "unit" (Pascal), a "class" (Eiffel), so on.
C++ has a much more primitive system of separate compilation, where essentially the compiler proper knows nothing about modules. The preprocessor can drag in text from "headers", which provides a way to get consistent declarations of things. And to easily get those declarations.
But that's all, and the current primitive text inclusion system has a large number problems, most manifestly visible via various compilers' support for so called "precompiled headers" (not part of the C++ standard!).
David Vandevoorde has worked on a module proposal for C++.
Sadly, it wasn't ready for C++11, but maybe it will come later.
*: In a comment David Rodriguez adds that “it is not really Andrew's fault. He only intended ADL for operators, the committee extended that to all functions”.
Consider this bit of code:
#include <iostream>
#include <string>
int main()
{
std::string str;
std::cout << "Enter a string: \n";
getline(std::cin, str);
}
Why must I use std:: for string, cin and cout, but not getline()? Is getline() not in the standard library? I'm actually somewhat confused why I can't just write using namespace std; and not have to #include anything in the standard library, too. Thanks in advance!
Re the magic automatic namespace qualification.
It's Andrew Koenig's* fault.
He considered the problem of providing operators for user defined types. And came up with the idea of resolving the function depending on its arguments. E.g. with an argument of type defined in namespace std, the compiler looks (also) in namespace std.
That's called either Koenig lookup or ADL, short for Argument Dependent Lookup.
Re using namespace std;.
For short exploratory programs you not only can, but to save work you should, do this.
However, the standard library defines some identifiers such as distance that are very likely to collide with names you use.
Hence, for more serious code consider whether other means of avoiding verbosity, might be better. Many programmers even maintain that you should not use using namespace std; in serious code. Some Norwegian firms have this as a coding guideline (I don't agree with so absolute a view, but you should know that it's there, and may be a majority view).
At any rate:
Never put a using namespace std; in the global namespace in a header
because that makes name collisions like distance very likely for some code that includes the header.
Why using doesn't make things accessible.
Many programming languages, such as Java and C# and Ada and Modula-2 and UCSD Pascal, just to name some, have language support for separately compiled modules, where a module consists of functions, variables, types, constants, maybe more.
Depending on the language a module may be called a "module" (Modula-2), a "package" (Ada, Java), a "unit" (Pascal), a "class" (Eiffel), so on.
C++ has a much more primitive system of separate compilation, where essentially the compiler proper knows nothing about modules. The preprocessor can drag in text from "headers", which provides a way to get consistent declarations of things. And to easily get those declarations.
But that's all, and the current primitive text inclusion system has a large number problems, most manifestly visible via various compilers' support for so called "precompiled headers" (not part of the C++ standard!).
David Vandevoorde has worked on a module proposal for C++.
Sadly, it wasn't ready for C++11, but maybe it will come later.
*: In a comment David Rodriguez adds that “it is not really Andrew's fault. He only intended ADL for operators, the committee extended that to all functions”.
I'm beginner at C++ so if the answer is obvious it possibly is the one I'm looking for. I was reading the second response in this thread and got confused.
#include <algorithm>
#include <cassert>
int
main()
{
using std::swap;
int a(3), b(5);
swap(a, b);
assert(a == 5 && b == 3);
}
What I don't get is "This is just a defined function. What I meant was, why it is not directly built-in" but there was no need to include a new library so isn't it built-in? Does the std library automatically get imported (if yes, why doesn't the namespace automatically get set to std)?
"This is just a defined function. What I meant was, why it is not directly built-in" but there was no need to import a new library so isn't it built-in? Does the std library automatically get imported (if yes, why doesn't the namespace automatically get set to std)?
Well, by defined function it means, most likely that the function is already pre-written, and defined in the library, it isn't directly built-in probably because it was designed that way; only core essentials were included in the language, and everything else is in a library so the programmer can import what they want.
By built-in, usually it's a keyword, like for or while.
And no, std isn't automatically imported since it's designed so the programmer can choose what namespaces they want, like custom namespaces or std. An example of this being bad to automatically have std is this:
Say you automatically defined std, then you wanted to do using namespace foo;, now if foo also had function cout, you would run into a huge problem, like say you wanted to do this;
// code above
cout << "Hello, World" << endl;
// code below
how would the compiler which namespace function to use use? the default or your foo namespace cout? In order to prevent this, there is no default namespace set, leaving it up to the programmer.
Hope that helps!
This is just a defined function. What I meant was, why it is not directly built-in" but there was no need to include a new library so isn't it built-in?
The C++ library is a part of C++, by definition. However, it is not a part of the core language. C++ is a huge, huge language. Making it so the compiler knew every nook and cranny of the language right off the bat would make the compiler huge and slow to load. The philosophy instead is to keep the core somewhat small and give programmers the ability to extend the functionality by #including header files that. specify what they need.
Why doesn't the namespace automatically get set to std?
That would essentially make all kinds of very common words into keywords. The list of words you shouldn't use (keywords, global function in C, words reserved by POSIX or Microsoft, ...) is already huge. Putting the C++ standard library in namespace std is a feature. Putting all of those names in the global namespace would be a huge misfeature.
In your code, you have the line:
using std::swap;
So the call to swap doesn't need std::. For assert, it was defined as a macro, so it also would not need std::. If you did not use using, then other than macros, you would have to use std:: to refer to functions and objects provided by the standard C++ library.
The standard C++ library normally get linked into your program when you compile your program to create an executable. From this point of view, you might consider it "built-in". However, the term "built-in" would usually mean the compiler treats the word swap like a keyword, which is not the case here. swap is a template function defined in the algorithm header file, and assert is a macro defined in cassert.
A namespace is a convenience to allow parts of software to be easily partitioned by name. So if you wanted to define your own swap function, you could put it into your own namespace.
namespace mine {
template <typename T> void swap (T &a, T &b) { /*...*/ }
}
And it would not collide with the standard, or with some library that defined swap without a namespace.