Namespace is a group of functions, classes, constants and variables.
Namespaces are open, i.e. you can define different members of the namespace in different files.
namespace ui
{
class Button;
}
using namespace
using namespace std
brings all the names from the
std
namespaces in the the current scope - namespace,
function, block.
using namespace
It is a really bad practice to use using namespace
at
anything but function or block level in headers.
You never know where this header is going to be included.
using namespace
It is a good practice to avoid using namespace
at
anything but function or block level.
Otherwise the names may clash in a unity build
Building the whole program/library from a single (or reduced
number of) translation units. It is implemented as
#include
-ing multiple C++ source files in a single C++
file and compiling it instead of the original files.
namespace
without a namenamespace {
int aswer();
}
What is the interface of a type T?
Calling a function without explicitly specifying the namespace of the function.
The compiler searches matching function declarations for an unqualified call in the current namespace and in the associated namespaces of each argument type for the call
namespace math {
class Polynom {
};
std::ostream& operator<< (std::ostream& output,
const Polynom& p)
{
// ...
}
void swap(Polynom& l, Polynom& r);
}
int main() {
Polynom p;
// finds the operator << in math
std::cout << p << std::endl;
}
use function
tricktemplate <typename T, typename A, typename I>
void fast_erase(std::vector<T, A>& v, I iterator)
{
// bring all std swap overloads
using std::swap;
// ADL finds math::swap(Polynom&, Polynom&)
swap(*iterator, v.back());
v.pop_back();
}
std::vector<Polynom> v;
fast_erase(v, v.begin());