Unable to understand what a using directive is/does.

I am attempting to find out what "#include <iostream>" and "using namespace std;" accomplish. I have gained a very basic understanding of include directives, header files, etc.

However, I'm having much more trouble comprehending using directives. I found a definition of using directive which states that a using directive "provides access to all namespace qualifiers and the scope operator." I don't know what a qualifier is/does or what a scope operator is/does; and I am hoping that someone can explain these things and prevent me from continually backtracking through C++ components and concepts that I don't know anything about.

I also do not understand the difference between a header file and a namespace.
I am attempting to find out what "#include <iostream>" and "using namespace std;" accomplish


Anytime you #include something, you're telling the preprocessor (big word, don't sweat it) to treat the contents of the file you're including as if they had appeared in the source program at the point where you included them.

The reason you'd want to do this, is so that you can write code in more than one file, and include it where other code in other files might need to know what you've written elsewhere.

Why would you want to break up your project into multiple files? Several reasons, I'll just redirect you to this article:
http://www.cplusplus.com/forum/articles/10627/

Now, on to namespaces.
I guess you could say that a namespace is just a convenient way of "grouping" things together that belong together. Just for the sake of example, let's say I'm writing an arithmetic library. I've written some some functions such as subtract() and add(). I then publish the library, and anyone who wants to use it, can. But Oh no! What if one of my users links my library with their project, and they already have a subtract() function of their own? We can't have clashing names. This is an example of when you might want to use a namespace.

In my case, I could simply put all my arithmetic related functions into their own namespace.
Here are my functions in the "global namespace":

1
2
3
4
5
6
7
int subtract(int a, int b) {
//Do things
}

int add(int a, int b) {
//Do things
}


Here they are in my arithmetic namespace, which I've elected to name "ar".

1
2
3
4
5
6
7
8
9
10
namespace ar {

	int subtract(int a, int b) {
		// Do things
	}

	int add(int a, int b) {
		// Do things
	}
}


This way, anyone who uses my arithmetic functions, can have their own subtract or add functions, without clashing identifiers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "arithmetic_header.h"//include my arithmetic header; part of my library!

int subtract(int a, int b) {
	// This is the function with the same name, that the user wrote.
	// Do something else
}

int main() {

	int a = subtract(1, 2);		// Call user defined function
	int b = ar::subtract(1, 2);	// Call to my library defined function

	std::cin.get();
	return 0;
}


This is super simplified, but I hope you got the idea. If there's anything confusing, feel free to ask.
Last edited on
Currently the only difference between a header file and a namespace that I've been able to find is:

A header file works with the preprocessor, whereas a namespace works with the compiler (I'd like to know why this is, unless it will just complicate things).

Why would "using namespace iostream" or "#include <std>" be invalid?
It's invalid because "iostream" is not a standard namespace, and "std" is not a file or directory.

These two things are entirely different concepts.

the iostream header (iostream.h) defines the standard input and output stream objects, which happen to be in the "std" namespace (std is the standard namespace).

Let's say I have a project with three files:
main.cpp
class.h
class.cpp

in main.cpp I have my main() function
in class.h I have a class declaration
in class.cpp I have a class implementation

The only I can use my class in the main function, is to include "class.h".
Last edited on
Why would "using namespace iostream" or "#include <std>" be invalid?


"iostream" is not a namespace( there is no namespace iostream { ... } ), it is a file, therefore using namespace iostream is invalid.


"std" is a namespace, which contains standard c++ classes, templates etc...
it is not a file ( similarly, there is no std.h ), so you cannot include it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

just remember, a header is just a file( a plain text file ), just like the one you saved from notepad, just with a diff extension etc..

a namespace is a "set" of class, templates etc.., it is used to group data's together.

To be able to access a function inside a namespace, we have to include the file in which it was declared,
then, when calling the function inside a namespace, precede it with the name of the namespace followed by the scope operator ( :: ).

That's why we call cout by std::cout
because cout is inside the namespace std. ( well maybe you just call cout probably because a using namespace std; is declared on top of your code )

about using namespace std;

using namespace is a special syntax in order for the compiler to "see" the functions inside the declared namespace, so you don't actually have to use the syntax std::cout

1
2
3
4
5
6
7
8
#include <iostream> // include the file named "iostream"

int main ()
{
    std::cout << "Hello World !"; // access the cout function ( object ) inside the namespace std
                                                  // if you don't want to precede every cout with std::, that is where
                                                  // you declare using namespace std;
}
Last edited on
Last question:

If an include directive is a directive that tells the preprocessor "to treat the contents of the file you're including as if they had appeared in the source program at the point where you included them."

Can you explain using directives in that way?

(Using directive is a directive that tells the compiler to:" ")

Thank you for your help, this has done a lot to clarify several points regarding #include and namespace.

edit: grammar
Last edited on
If an include directive is a directive that tells the preprocessor "to treat the contents of the file you're including as if they had appeared in the source program at the point where you included them."


more precisely it replaces the #include directive with the actual file contents

~~~~~~~~

Now regarding your question:

(Using directive is a directive that tells the compiler to:" ")


i think you mean :

Using include directive is a directive that tells the compiler to:
-> replace the include directive w/ the actual file contents

just a little lesson:

a preprocessor directive ( or simply directive ) is a special command for the compiler to do something to your file, code ( or whatever ) before compilation :

->
samples of preprocessor directive :
1
2
3
4
5
6
7
8
9
10
11
#pragma once // directive to tell the compiler to include this file once

#include <iostream> // directive to include the file named "iostream"

#define PI 3.14 // directive to replace every occurence of PI in you code to 3.14 ( this is what really happens when you #define )

#ifndef _WIN32 // if _WIN32 is not defined, do not compile the code beetween this and #endif
                          // edit: the correct term is: only if _WIN32 is not defined, then do process the following code
                          // thanks @NT3
// .. 
#endif 


Hope this helps !!!
Last edited on
As has been said multiple times:

A using directive is a directive that tells the compiler to act as if you are typing that name before every function or variable that is part of the namespace, by first checking the global namespace for the function, and then (if it doesn't exist) checks the namespace you declared.

If that is a bit confusing, here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
    std::cout << "Hello!" << std::endl;  // Access the "cout" and "endl"
                                         // identifiers from within the "std" namespace.

    cout << "Hello!" << endl;  // Error: "cout" is undeclared.

    using namespace std;   // Allow the compiler to "see" in the "std" namespace

    cout << "Hello!" << endl;  // cout is undeclared in the global namespace, so the
                               // compiler looks in the "std" namespace, and finds the
                               // function that it is looking for.

    return 0;
}


EDIT:
@Shadow Fiend, on line 7 of your example, I think you meant to say this:
1
2
3
#ifndef _WIN32 // only if _WIN32 is not defined, then do process the following code
//...
#endif 
Last edited on
i think you mean:


When I said using directive i meant to refer to something like: "using namespace std;". I don't know what the correct term for that is.
@NT3, yes that is what i mean :)))
A using directive is a directive that tells the compiler to act as if you are typing that name before every function or variable that is part of the namespace, by first checking the global namespace for the function, and then (if it doesn't exist) checks the namespace you declared.

Exactly what I was after, thank you (and thanks for the example, I tend to grasp "illustrated" concepts more easily).
Topic archived. No new replies allowed.