} that closes the function definition), but not outside it:1
2
3
4
5
6
7
8
9
10
11
12
13
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from this function
}
1
2
3
4
5
6
7
int some_function ()
{
int x;
x = 0;
double x; // wrong: name already used in this scope
x = 0.0;
}
// inner block scopes
#include <iostream>
using namespace std;
int main () {
int x = 10;
int y = 20;
{
int x; // ok, inner scope.
x = 50; // sets value to inner x
y = 50; // sets value to (outer) y
cout << "inner block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
}
cout << "outer block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
return 0;
}
inner block: x: 50 y: 50 outer block: x: 10 y: 50
y is not hidden in the inner block, and thus accessing y still accesses the outer variable.
namespace identifier
{
named_entities
}
identifier is any valid identifier and named_entities is the set of variables, types and functions that are included within the namespace. For example:1
2
3
4
namespace myNamespace
{
int a, b;
}
a and b are normal variables declared within a namespace called myNamespace.a or b), but if accessed from outside the myNamespace namespace they have to be properly qualified with the scope operator ::. For example, to access the previous variables from outside myNamespace they should be qualified like:1
2
myNamespace::a
myNamespace::b
// namespaces
#include <iostream>
using namespace std;
namespace foo
{
int value() { return 5; }
}
namespace bar
{
const double pi = 3.1416;
double value() { return 2*pi; }
}
int main () {
cout << foo::value() << '\n';
cout << bar::value() << '\n';
cout << bar::pi << '\n';
return 0;
}
5 6.2832 3.1416
value. One is defined within the namespace foo, and the other one in bar. No redefinition errors happen thanks to namespaces. Notice also how pi is accessed in an unqualified manner from within namespace bar (just as pi), while it is again accessed in main, but here it needs to be qualified as bar::pi.1
2
3
namespace foo { int a; }
namespace bar { int b; }
namespace foo { int c; }
a and c are in namespace foo, while b is in namespace bar. Namespaces can even extend across different translation units (i.e., across different files of source code).using introduces a name into the current declarative region (such as a block), thus avoiding the need to qualify the name. For example:// using
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using first::x;
using second::y;
cout << x << '\n';
cout << y << '\n';
cout << first::y << '\n';
cout << second::x << '\n';
return 0;
}
5 2.7183 10 3.1416
main, the variable x (without any name qualifier) refers to first::x, whereas y refers to second::y, just as specified by the using declarations. The variables first::y and second::x can still be accessed, but require fully qualified names.using can also be used as a directive to introduce an entire namespace:// using
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
cout << x << '\n';
cout << y << '\n';
cout << second::x << '\n';
cout << second::y << '\n';
return 0;
}
5 10 3.1416 2.7183
first, all direct uses of x and y without name qualifiers were also looked up in namespace first.using and using namespace have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope. For example, it would be possible to first use the objects of one namespace and then those of another one by splitting the code in different blocks:// using namespace example
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
}
namespace second
{
double x = 3.1416;
}
int main () {
{
using namespace first;
cout << x << '\n';
}
{
using namespace second;
cout << x << '\n';
}
return 0;
}
5 3.1416
namespace new_name = current_name;
std namespace. Most examples in these tutorials, in fact, include the following line:1
using namespace std;
std namespace into the code. This is done in these tutorials to facilitate comprehension and shorten the length of the examples, but many programmers prefer to qualify each of the elements of the standard library used in their programs. For example, instead of:1
cout << "Hello world!";
1
std::cout << "Hello world!";
std namespace are introduced with using declarations or are fully qualified on every use does not change the behavior or efficiency of the resulting program in any way. It is mostly a matter of style preference, although for projects mixing libraries, explicit qualification tends to be preferred.// static vs automatic storage
#include <iostream>
using namespace std;
int x;
int main ()
{
int y;
cout << x << '\n';
cout << y << '\n';
return 0;
}
0 4285838
x is guaranteed to be zero. y can actually contain just about any value (including zero).