Using Std Namespace

Why is using namespace std; considered bad practice?

Consider two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, and you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.

using std::type v.s. using std namespace [duplicate]

It depends.

If you want to inject a single name into another scope, the using-declaration is better, e.g.

namespace foolib
{
// allow vector to be used unqualified within foo,
// or used as foo::vector
using std::vector;

vector<int> vec();

template<typename T> struct Bar { T t; };

template<typename T>
void swap(Bar<T>& lhs, Bar<T>& rhs)
{
using std::swap;
// find swap by ADL, otherwise use std::swap
swap(lhs.t, rhs.t);
}
}

But sometimes you just want all names, which is what a using-directive does. That could be used locally in a function, or globally in a source file.

Putting using namespace outside a function body should only be done where you know exactly what's being included so it's safe (i.e. not in a header, where you don't know what's going to be included before or after that header) although many people still frown on this usage (read the answers at Why is "using namespace std" considered bad practice? for details):

#include <vector>
#include <iostream>
#include "foolib.h"
using namespace foo; // only AFTER all headers

Bar<int> b;

A good reason to use a using-directive is where the namespace only contains a small number of names that are kept intentionally segregated, and are designed to be used by using-directive:

#include <string>
// make user-defined literals usable without qualification,
// without bringing in everything else in namespace std.
using namespace std::string_literals;
auto s = "Hello, world!"s;

So there is no single answer that can say one is universally better than the other, they have different uses and each is better in different contexts.

Regarding the first usage of using namespace, the creator of C++, Bjarne Stroustrup, has this to say in §14.2.3 of The C++ Programming Language, 4th Ed (emphasis mine):

Often we like to use every name from a namespace without qualification. That can be achieved by providing a using-declaration for each name from the namespace, but that's tedious and requires extra work each time a new name is added to or removed from the namespace. Alternatively, we can use a using-directive to request that every name from a namespace be accessible in our scope without qualification. [...]

[...] Using a using-directive to make names from a frequently used and well-known library available without qualification is a popular technique for simplifying code. This is the technique used to access standard-library facilities throughout this book. [...]

Within a function, a using-directive can be safely used as a notational convenience, but care should be taken with global using-directives because overuse can lead to exactly the name clashes that namespaces were introduced to avoid. [...]

Consequently, we must be careful with using-directives in the global scope. In particular, don't place a using-directive in the global scope in a header file except in very specialized circumstances (e.g. to aid transition) because you never know where a header might be #included.

To me this seems far better advice than just insisting it is bad and should not be used.

What is the function of using namespace:std; in C++? [duplicate]

One concept in c++ are namespaces. This organizes your code in a way.

What is using namespace std; do now? Let us explore this by example.

#include <iostream>
int main() {
std::cout << "Hello World" << std::endl; // important line
return 0;
}

You see the std keyword in the line with the comment. This is called a namespace. So you tell the compiler, you want to use cout from the namespace std.

With using namespace std;

#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl; // important line
return 0;
}

There you tell the compiler, to open the std namespace four you in this scope. So you can use cout without the std::. This could be mistaken to be more efficent to code. But you will run into problems at one point.

In the namespace std are hunderts or thounsands of functions or constants and so on defined. Most of the time you won't bother with that. But if you define a function with the same name and parameters at one point, you hardly will be able to find the mistake. For example there is std::find. There may be a chance you define the same function. The compiler errors in this case are a pain. So I would strongly discurage you to use using namespace std;.

C++: Questions about using namespace std and cout [duplicate]

cout is a global object defined in the std namespace, and endl is a (stream manipulator) function also defined in the std namespace.

If you take no action to import their names into the global namespace, you won't be able to refer to them with the unqualified identifiers cout and endl. You have to use the fully qualified names:

std::cout << "Hello, World!" << std::endl;

Basically, what using namespace std does is to inject all the names of entities that exist in the std namespace into the global namespace:

using namespace std;
cout << "Hello, Wordl!" << endl;

However, keep in mind that have such a using directive in the global namespace is a BAD programming practice, which will almost certainly lead to evil name clashes.

If you really need to use it (e.g. if a function of yours is using many functions defined in the std namespace, and writing std:: makes the code harder to read), you should rather restrict its scope to the local scope of individual functions:

void my_function_using_a_lot_of_stuff_from_std()
{
using namespace std;
cout << "Hello, Wordl!" << endl;

// Other instructions using entities from the std namespace...
}

Much better, as long as this is practical, is to use the following, less invasive using declarations, which will selectively import only the names you specify:

using std::cout;
using std::endl;

cout << "Hello, Wordl!" << endl;

What is using namespace::std in C++

using namespace::std is the same as using namespace std;

The :: symbol is the scope resolution operator. When used without a scope name before it , it refers to the global namespace. This means that std is a top level namespace, and is not enclosed in another.

The spaces before and after the :: are optional in this case because the lexer can deduce the tokens from the context.

For example, all of the following are valid:

namespace A { namespace notstd{} } // define my own namespaces A and A::notstd
using namespace::std; // the standard library std
using namespace A;
using namespace ::A;
using namespace::A;
using namespace A::notstd;

Update:

As noted in one of the comments, using namespace ::std; and using namespace std; may actually lead to different results if the statement comes inside another namespace which contains its own nested namespace std. See the following (highly unlikely) example:

#include <stdio.h>

namespace A {
namespace std {
int cout = 5;
}
using namespace std;
void f1() {
cout++;
}
}

int main()
{
A::f1();
printf("%d\n",A::std::cout); // prints 6

return 0;
}


Related Topics



Leave a reply



Submit