My question is how does usingnamespace std; fit into what I've learned from the tutorial.
After reading the tutorial I'd think setting up the standard namespace would look something like this:
using std = namespace;
or
typedefnamespace std;
I'm missing something here and if anybody can explain the part I'm not understanding (or a link to a manual) about why it's written usingnamespace std; I'd be grateful.
Basically speaking, cout and cin are actually "supposed" to be std::cout and std::cin, since they are both within the std namespace.
So a simple program might look like this:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
std::cout << "Please enter a number: ";
int number;
std::cin >> number;
std::cout << "You entered " << number << std::endl;
//cout << "Hi again"; // Error: what's 'cout' ?
}
Notice that I had to put std:: in front of cout, cin, and endl, since they are all in the std namespace.
Now, let's say that I'm super lazy and I don't like having to type std:: all the time.
By putting usingnamespace std; in my code, it basically lets me drop the std:: prefix so I don't have to type it anymore:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
usingnamespace std;
int main()
{
cout << "Hello world!" << endl; // Okay: the compiler knows I mean 'std::cout' and 'std::endl'
cout << "Please enter a number: ";
int number;
cin >> number;
cout << "You entered " << number << std::endl; // But you can still put std:: if you want
}
I see, so if I understood what I read. You don't have to explicitly declare a type when using the key word using?
I could do something like
using C;
or
using pChar;
which would use the same mechanism as usingnamespace std;
but how does the type get defined later on in a program?
I have been avoiding using usingnamespace std; but I'd still like to understand how the key word using works which is where my questions are coming from. usingnamespace std; just seemed like the most ubiquitous example of using I could think of in my limited experience.
Err, usingnamespace std; just makes it so that you don't have to type std:: when accessing functions/variables/types within the std namespace. (Normally, you would have to type e.g. std::cout, but with usingnamespace std; you can just type cout and the compiler will know that you mean std::cout)
Namespaces aren't types.
Now, in C++11, they added a way to alias types with the using keyword: using myIntNickname = int;
The above would be identical to typedefint myIntNickname;
But that's a completely unrelated usage of using.
You can actually do something like using std::cout;.
This is basically just like usingnamespace std;, except only for std::cout.
That is, it lets you type cout without having to prefix it with std::, but everything else in the std namespace still has to have std:: in front of it.
Your two examples, using C; and using pChar;, would not be valid C++ code.
Now, using C = char; and using pChar = char*; (for instance) would be (it defines alias of char and char*), as well as using C::foo; and using pChar::bar; (if C and pChar are namespaces and foo is a member of namespace C and bar is a member of namspace pChar. Why you would want to name your namespaces C or pChar, I have no idea).
so basically the compiler treats the key word using differently depending on how the code is written in C++. Ie the compiler would use one set of algorithms for usingnamespace XYZ and a completely different and unrelated set of algorithms for using C = char;
Essentially it is an overloaded key word?
That's also kind of a nice trick to know there LB. Thanks for sharing :)