Rectangle 27 67

Do not use it globally

It is considered "bad" only when used globally. Because:

  • You clutter the namespace you are programming in.
  • Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz.
  • Whatever is true for other readers of your source code is even more true for the most frequent reader of it: yourself. Come back in a year or two and take a look...
  • If you only talk about using namespace std you might not be aware of all the stuff you grab -- and when you add another #include or move to a new C++ revision you might get name conflicts you were not aware of.

Go ahead and use it locally (almost) freely. This, of course, prevents you from repetition of std:: -- and repetition is also bad.

In C++03 there was an idiom -- boilerplate code -- for implementing a swap function for your classes. It was suggested that you actually use a local using namespace std -- or at least using std::swap:

class Thing {
    int    value_;
    Child  child_;
public:
    // ...
    friend void swap(Thing &a, Thing &b);
};
void swap(Thing &a, Thing &b) {
    using namespace std;      // make `std::swap` available
    // swap all members
    swap(a.value_, b.value_); // `std::stwap(int, int)`
    swap(a.child_, b.child_); // `swap(Child&,Child&)` or `std::swap(...)`
}

This does the following magic:

std::swap
value_
void std::swap(int, int)
  • If you have an overload void swap(Child&, Child&) implemented the compiler will choose it.
  • If you do not have that overload the compiler will use void std::swap(Child&,Child&) and try its best swapping these.

With C++11 there is no reason to use this pattern any more. The implementation of std::swap was changed to find a potential overload and choose it.

"The implementation of std::swap was changed to find a potential overload and choose it." - What? Are you sure about that? Though it is true that providing a custom swap in the first place isn't that much important in C++11 anymore, since the std::swap itself is more flexible (uses move semantics). But std::swap automatically chosing your own custom swap, that is absolutely new to me (and I don't really believe it).

@ChristianRau I think so, yes. I read this on SO somewhere. We can always ask Howard, he should know. I am digging and digging now...

Even in the swap case, the clearer (and thankfully more common) idiom is to write using std::swap; rather than using namespace std;. The more specific idiom has fewer side effects and therefore makes the code more maintainable.

The final sentence is wrong. In C++11 the Std Swap Two Step was officially blessed as the right way to call swap, and various other places in the standard were changed to say they call swap like that (N.B. as stated above, using std::swap is the right way, not using namespace std). But std::swap itself was emphatically not changed to find some other swap and use it. If std::swap gets called, then std::swap gets used.

It might be wiser to just type using std::swap locally though, to reduce the local namespace while at the same time creating self-documenting code. You are rarely ever interested in the whole std namespace, so just out pick out the parts you are interested in.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 64

Do not use it globally

It is considered "bad" only when used globally. Because:

  • You clutter the namespace you are programming in.
  • Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz.
  • Whatever is true for other readers of your source code is even more true for the most frequent reader of it: yourself. Come back in a year or two and take a look...
  • If you only talk about using namespace std you might not be aware of all the stuff you grab -- and when you add another #include or move to a new C++ revision you might get name conflicts you were not aware of.

Go ahead and use it locally (almost) freely. This, of course, prevents you from repetition of std:: -- and repetition is also bad.

In C++03 there was an idiom -- boilerplate code -- for implementing a swap function for your classes. It was suggested that you actually use a local using namespace std -- or at least using std::swap:

class Thing {
    int    value_;
    Child  child_;
public:
    // ...
    friend void swap(Thing &a, Thing &b);
};
void swap(Thing &a, Thing &b) {
    using namespace std;      // make `std::swap` available
    // swap all members
    swap(a.value_, b.value_); // `std::stwap(int, int)`
    swap(a.child_, b.child_); // `swap(Child&,Child&)` or `std::swap(...)`
}

This does the following magic:

std::swap
value_
void std::swap(int, int)
  • If you have an overload void swap(Child&, Child&) implemented the compiler will choose it.
  • If you do not have that overload the compiler will use void std::swap(Child&,Child&) and try its best swapping these.

With C++11 there is no reason to use this pattern any more. The implementation of std::swap was changed to find a potential overload and choose it.

"The implementation of std::swap was changed to find a potential overload and choose it." - What? Are you sure about that? Though it is true that providing a custom swap in the first place isn't that much important in C++11 anymore, since the std::swap itself is more flexible (uses move semantics). But std::swap automatically chosing your own custom swap, that is absolutely new to me (and I don't really believe it).

@ChristianRau I think so, yes. I read this on SO somewhere. We can always ask Howard, he should know. I am digging and digging now...

Even in the swap case, the clearer (and thankfully more common) idiom is to write using std::swap; rather than using namespace std;. The more specific idiom has fewer side effects and therefore makes the code more maintainable.

The final sentence is wrong. In C++11 the Std Swap Two Step was officially blessed as the right way to call swap, and various other places in the standard were changed to say they call swap like that (N.B. as stated above, using std::swap is the right way, not using namespace std). But std::swap itself was emphatically not changed to find some other swap and use it. If std::swap gets called, then std::swap gets used.

It might be wiser to just type using std::swap locally though, to reduce the local namespace while at the same time creating self-documenting code. You are rarely ever interested in the whole std namespace, so just out pick out the parts you are interested in.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 64

Do not use it globally

It is considered "bad" only when used globally. Because:

  • You clutter the namespace you are programming in.
  • Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz.
  • Whatever is true for other readers of your source code is even more true for the most frequent reader of it: yourself. Come back in a year or two and take a look...
  • If you only talk about using namespace std you might not be aware of all the stuff you grab -- and when you add another #include or move to a new C++ revision you might get name conflicts you were not aware of.

Go ahead and use it locally (almost) freely. This, of course, prevents you from repetition of std:: -- and repetition is also bad.

In C++03 there was an idiom -- boilerplate code -- for implementing a swap function for your classes. It was suggested that you actually use a local using namespace std -- or at least using std::swap:

class Thing {
    int    value_;
    Child  child_;
public:
    // ...
    friend void swap(Thing &a, Thing &b);
};
void swap(Thing &a, Thing &b) {
    using namespace std;      // make `std::swap` available
    // swap all members
    swap(a.value_, b.value_); // `std::stwap(int, int)`
    swap(a.child_, b.child_); // `swap(Child&,Child&)` or `std::swap(...)`
}

This does the following magic:

std::swap
value_
void std::swap(int, int)
  • If you have an overload void swap(Child&, Child&) implemented the compiler will choose it.
  • If you do not have that overload the compiler will use void std::swap(Child&,Child&) and try its best swapping these.

With C++11 there is no reason to use this pattern any more. The implementation of std::swap was changed to find a potential overload and choose it.

"The implementation of std::swap was changed to find a potential overload and choose it." - What? Are you sure about that? Though it is true that providing a custom swap in the first place isn't that much important in C++11 anymore, since the std::swap itself is more flexible (uses move semantics). But std::swap automatically chosing your own custom swap, that is absolutely new to me (and I don't really believe it).

@ChristianRau I think so, yes. I read this on SO somewhere. We can always ask Howard, he should know. I am digging and digging now...

Even in the swap case, the clearer (and thankfully more common) idiom is to write using std::swap; rather than using namespace std;. The more specific idiom has fewer side effects and therefore makes the code more maintainable.

The final sentence is wrong. In C++11 the Std Swap Two Step was officially blessed as the right way to call swap, and various other places in the standard were changed to say they call swap like that (N.B. as stated above, using std::swap is the right way, not using namespace std). But std::swap itself was emphatically not changed to find some other swap and use it. If std::swap gets called, then std::swap gets used.

It might be wiser to just type using std::swap locally though, to reduce the local namespace while at the same time creating self-documenting code. You are rarely ever interested in the whole std namespace, so just out pick out the parts you are interested in.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 66

Do not use it globally

It is considered "bad" only when used globally. Because:

  • You clutter the namespace you are programming in.
  • Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz.
  • Whatever is true for other readers of your source code is even more true for the most frequent reader of it: yourself. Come back in a year or two and take a look...
  • If you only talk about using namespace std you might not be aware of all the stuff you grab -- and when you add another #include or move to a new C++ revision you might get name conflicts you were not aware of.

Go ahead and use it locally (almost) freely. This, of course, prevents you from repetition of std:: -- and repetition is also bad.

In C++03 there was an idiom -- boilerplate code -- for implementing a swap function for your classes. It was suggested that you actually use a local using namespace std -- or at least using std::swap:

class Thing {
    int    value_;
    Child  child_;
public:
    // ...
    friend void swap(Thing &a, Thing &b);
};
void swap(Thing &a, Thing &b) {
    using namespace std;      // make `std::swap` available
    // swap all members
    swap(a.value_, b.value_); // `std::stwap(int, int)`
    swap(a.child_, b.child_); // `swap(Child&,Child&)` or `std::swap(...)`
}

This does the following magic:

std::swap
value_
void std::swap(int, int)
  • If you have an overload void swap(Child&, Child&) implemented the compiler will choose it.
  • If you do not have that overload the compiler will use void std::swap(Child&,Child&) and try its best swapping these.

With C++11 there is no reason to use this pattern any more. The implementation of std::swap was changed to find a potential overload and choose it.

"The implementation of std::swap was changed to find a potential overload and choose it." - What? Are you sure about that? Though it is true that providing a custom swap in the first place isn't that much important in C++11 anymore, since the std::swap itself is more flexible (uses move semantics). But std::swap automatically chosing your own custom swap, that is absolutely new to me (and I don't really believe it).

@ChristianRau I think so, yes. I read this on SO somewhere. We can always ask Howard, he should know. I am digging and digging now...

Even in the swap case, the clearer (and thankfully more common) idiom is to write using std::swap; rather than using namespace std;. The more specific idiom has fewer side effects and therefore makes the code more maintainable.

The final sentence is wrong. In C++11 the Std Swap Two Step was officially blessed as the right way to call swap, and various other places in the standard were changed to say they call swap like that (N.B. as stated above, using std::swap is the right way, not using namespace std). But std::swap itself was emphatically not changed to find some other swap and use it. If std::swap gets called, then std::swap gets used.

It might be wiser to just type using std::swap locally though, to reduce the local namespace while at the same time creating self-documenting code. You are rarely ever interested in the whole std namespace, so just out pick out the parts you are interested in.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 27

I agree that it should not be used globally, but it's not so evil to to use locally, like in a namespace. Here's an example from "The C++ Programming Language" :

namespace My_lib {

    using namespace His_lib; // everything from His_lib
    using namespace Her_lib; // everything from Her_lib

    using His_lib::String; // resolve potential clash in favor of His_lib
    using Her_lib::Vector; // resolve potential clash in favor of Her_lib

}

In this example, we resolved potential name clashes and ambiguities arising from their composition.

Names explicitly declared there (including names declared by using-declarations like His_lib::String) take priority over names made accessible in another scope by a using-directive (using namespace Her_lib).

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 25

I agree that it should not be used globally, but it's not so evil to to use locally, like in a namespace. Here's an example from "The C++ Programming Language" :

namespace My_lib {

    using namespace His_lib; // everything from His_lib
    using namespace Her_lib; // everything from Her_lib

    using His_lib::String; // resolve potential clash in favor of His_lib
    using Her_lib::Vector; // resolve potential clash in favor of Her_lib

}

In this example, we resolved potential name clashes and ambiguities arising from their composition.

Names explicitly declared there (including names declared by using-declarations like His_lib::String) take priority over names made accessible in another scope by a using-directive (using namespace Her_lib).

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 25

I agree that it should not be used globally, but it's not so evil to to use locally, like in a namespace. Here's an example from "The C++ Programming Language" :

namespace My_lib {

    using namespace His_lib; // everything from His_lib
    using namespace Her_lib; // everything from Her_lib

    using His_lib::String; // resolve potential clash in favor of His_lib
    using Her_lib::Vector; // resolve potential clash in favor of Her_lib

}

In this example, we resolved potential name clashes and ambiguities arising from their composition.

Names explicitly declared there (including names declared by using-declarations like His_lib::String) take priority over names made accessible in another scope by a using-directive (using namespace Her_lib).

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 26

I agree that it should not be used globally, but it's not so evil to to use locally, like in a namespace. Here's an example from "The C++ Programming Language" :

namespace My_lib {

    using namespace His_lib; // everything from His_lib
    using namespace Her_lib; // everything from Her_lib

    using His_lib::String; // resolve potential clash in favor of His_lib
    using Her_lib::Vector; // resolve potential clash in favor of Her_lib

}

In this example, we resolved potential name clashes and ambiguities arising from their composition.

Names explicitly declared there (including names declared by using-declarations like His_lib::String) take priority over names made accessible in another scope by a using-directive (using namespace Her_lib).

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 276

I think it's bad to put it in the header files of your classes: because then you would be forcing anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

However, you may feel free to put a using statement in your (private) *.cpp files.

Beware that some people disagree with my saying "feel free" like this -- because although a using statement in a cpp file is better than in a header (because it doesn't affect people who include your header file), they think it's still not good (because depending on the code it could make the implementation of the class more difficult to maintain). This FAQ topic says,

The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldnt use it on a regular basis, at least not in your new C++ code.

  • A using declaration: using std::cout; // a using-declaration lets you use cout without qualification cout << "Values:";
std::cout << "Values:";

It is less bad to put it in a .cpp file than in a header, but the problem stays the same for maintainability. It is taking the risk that two functions with the same name will clash when the code/the library you use/the C++ standard gets modified.

Have you tried this at all? Because the using namespace directive will not carry over into another file. (GCC 4.8+)

@zackery.fix, "Apple LLVM version 7.0.2 (clang-700.1.81)" propagates using namespace std; from header to source file, and I verified that GCC does not. So at least having the "using" directive in header is risky.

Yea.. I don't use LLVM or clang and this is not a standard approach anyway.

You really should not tell people to "feel free" with using namespace std in a .cpp file. @tienne is right.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 270

I think it's bad to put it in the header files of your classes: because then you would be forcing anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

However, you may feel free to put a using statement in your (private) *.cpp files.

Beware that some people disagree with my saying "feel free" like this -- because although a using statement in a cpp file is better than in a header (because it doesn't affect people who include your header file), they think it's still not good (because depending on the code it could make the implementation of the class more difficult to maintain). This FAQ topic says,

The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldnt use it on a regular basis, at least not in your new C++ code.

  • A using declaration: using std::cout; // a using-declaration lets you use cout without qualification cout << "Values:";
std::cout << "Values:";

It is less bad to put it in a .cpp file than in a header, but the problem stays the same for maintainability. It is taking the risk that two functions with the same name will clash when the code/the library you use/the C++ standard gets modified.

Have you tried this at all? Because the using namespace directive will not carry over into another file. (GCC 4.8+)

@zackery.fix, "Apple LLVM version 7.0.2 (clang-700.1.81)" propagates using namespace std; from header to source file, and I verified that GCC does not. So at least having the "using" directive in header is risky.

Yea.. I don't use LLVM or clang and this is not a standard approach anyway.

You really should not tell people to "feel free" with using namespace std in a .cpp file. @tienne is right.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 270

I think it's bad to put it in the header files of your classes: because then you would be forcing anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

However, you may feel free to put a using statement in your (private) *.cpp files.

Beware that some people disagree with my saying "feel free" like this -- because although a using statement in a cpp file is better than in a header (because it doesn't affect people who include your header file), they think it's still not good (because depending on the code it could make the implementation of the class more difficult to maintain). This FAQ topic says,

The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldnt use it on a regular basis, at least not in your new C++ code.

  • A using declaration: using std::cout; // a using-declaration lets you use cout without qualification cout << "Values:";
std::cout << "Values:";

It is less bad to put it in a .cpp file than in a header, but the problem stays the same for maintainability. It is taking the risk that two functions with the same name will clash when the code/the library you use/the C++ standard gets modified.

Have you tried this at all? Because the using namespace directive will not carry over into another file. (GCC 4.8+)

@zackery.fix, "Apple LLVM version 7.0.2 (clang-700.1.81)" propagates using namespace std; from header to source file, and I verified that GCC does not. So at least having the "using" directive in header is risky.

Yea.. I don't use LLVM or clang and this is not a standard approach anyway.

You really should not tell people to "feel free" with using namespace std in a .cpp file. @tienne is right.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 175

I recently ran into a complaint about VisualStudio2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and VisualStudio2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

This. Boost and std have a lot of overlap - especially since C++11.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 173

I recently ran into a complaint about VisualStudio2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and VisualStudio2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

This. Boost and std have a lot of overlap - especially since C++11.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 173

I recently ran into a complaint about VisualStudio2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and VisualStudio2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

This. Boost and std have a lot of overlap - especially since C++11.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 274

I think it's bad to put it in the header files of your classes: because then you would be forcing anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

However, you may feel free to put a using statement in your (private) *.cpp files.

Beware that some people disagree with my saying "feel free" like this -- because although a using statement in a cpp file is better than in a header (because it doesn't affect people who include your header file), they think it's still not good (because depending on the code it could make the implementation of the class more difficult to maintain). This FAQ topic says,

The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldnt use it on a regular basis, at least not in your new C++ code.

  • A using declaration: using std::cout; // a using-declaration lets you use cout without qualification cout << "Values:";
std::cout << "Values:";

It is less bad to put it in a .cpp file than in a header, but the problem stays the same for maintainability. It is taking the risk that two functions with the same name will clash when the code/the library you use/the C++ standard gets modified.

Have you tried this at all? Because the using namespace directive will not carry over into another file. (GCC 4.8+)

@zackery.fix, "Apple LLVM version 7.0.2 (clang-700.1.81)" propagates using namespace std; from header to source file, and I verified that GCC does not. So at least having the "using" directive in header is risky.

Yea.. I don't use LLVM or clang and this is not a standard approach anyway.

You really should not tell people to "feel free" with using namespace std in a .cpp file. @tienne is right.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 117

Short version: don't use global using declarations or directives in header files. Feel free to use them in implementation files. Here's what Herb Sutter and Andrei Alexandrescu have to say about this issue in C++ Coding Standards (bolding for emphasis is mine):

Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive.

Corollary: In header files, dont write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.)

Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable

Sad that this sane guidance is so buried under misguided answers.

Just one more programmer's opinion here, but while I agree 100% with the statement that the word using should never appear in a header, I'm not as convinced about the free license to place using namespace xyz; anywhere in your code, especially if xyz is std. I use the using std::vector; form, since that only pulls a single element from the namespace into pseudo-global scope, therefore leading to far less risk of a collision.

@Lightness Races in Orbit you are of course entitled to your opinion. Would have been more helpful if there had been some attempt at explanation why you do not agree with advice given in this answer. Especially would be interesting to understand what is the point of namespaces if 'using' them is bad? Why not just name things std_cout instead of std::cout ... the creators of C++/namespace must have had some idea when they bothered to create them.

@nyholku: No need - the majority of the other answers give the same reasons I would. Also please do no hesitate to note the ":)" I appended to my comment! And that I didn't say namespaces are bad.

Yeah, I noticed that :) but IMO the majority of answer (that go against this sage advice) are misguided (not that I made any statistics what is the majority now). If you agree that namespace are 'not bad' then you might say where you think they are appropriate if you disagree with this answer?

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 112

Short version: don't use global using declarations or directives in header files. Feel free to use them in implementation files. Here's what Herb Sutter and Andrei Alexandrescu have to say about this issue in C++ Coding Standards (bolding for emphasis is mine):

Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive.

Corollary: In header files, dont write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.)

Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable

Sad that this sane guidance is so buried under misguided answers.

Just one more programmer's opinion here, but while I agree 100% with the statement that the word using should never appear in a header, I'm not as convinced about the free license to place using namespace xyz; anywhere in your code, especially if xyz is std. I use the using std::vector; form, since that only pulls a single element from the namespace into pseudo-global scope, therefore leading to far less risk of a collision.

@Lightness Races in Orbit you are of course entitled to your opinion. Would have been more helpful if there had been some attempt at explanation why you do not agree with advice given in this answer. Especially would be interesting to understand what is the point of namespaces if 'using' them is bad? Why not just name things std_cout instead of std::cout ... the creators of C++/namespace must have had some idea when they bothered to create them.

@nyholku: No need - the majority of the other answers give the same reasons I would. Also please do no hesitate to note the ":)" I appended to my comment! And that I didn't say namespaces are bad.

Yeah, I noticed that :) but IMO the majority of answer (that go against this sage advice) are misguided (not that I made any statistics what is the majority now). If you agree that namespace are 'not bad' then you might say where you think they are appropriate if you disagree with this answer?

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 112

Short version: don't use global using declarations or directives in header files. Feel free to use them in implementation files. Here's what Herb Sutter and Andrei Alexandrescu have to say about this issue in C++ Coding Standards (bolding for emphasis is mine):

Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive.

Corollary: In header files, dont write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.)

Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable

Sad that this sane guidance is so buried under misguided answers.

Just one more programmer's opinion here, but while I agree 100% with the statement that the word using should never appear in a header, I'm not as convinced about the free license to place using namespace xyz; anywhere in your code, especially if xyz is std. I use the using std::vector; form, since that only pulls a single element from the namespace into pseudo-global scope, therefore leading to far less risk of a collision.

@Lightness Races in Orbit you are of course entitled to your opinion. Would have been more helpful if there had been some attempt at explanation why you do not agree with advice given in this answer. Especially would be interesting to understand what is the point of namespaces if 'using' them is bad? Why not just name things std_cout instead of std::cout ... the creators of C++/namespace must have had some idea when they bothered to create them.

@nyholku: No need - the majority of the other answers give the same reasons I would. Also please do no hesitate to note the ":)" I appended to my comment! And that I didn't say namespaces are bad.

Yeah, I noticed that :) but IMO the majority of answer (that go against this sage advice) are misguided (not that I made any statistics what is the majority now). If you agree that namespace are 'not bad' then you might say where you think they are appropriate if you disagree with this answer?

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 174

I recently ran into a complaint about VisualStudio2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and VisualStudio2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

This. Boost and std have a lot of overlap - especially since C++11.

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq
Rectangle 27 115

Short version: don't use global using declarations or directives in header files. Feel free to use them in implementation files. Here's what Herb Sutter and Andrei Alexandrescu have to say about this issue in C++ Coding Standards (bolding for emphasis is mine):

Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive.

Corollary: In header files, dont write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.)

Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable

Sad that this sane guidance is so buried under misguided answers.

Just one more programmer's opinion here, but while I agree 100% with the statement that the word using should never appear in a header, I'm not as convinced about the free license to place using namespace xyz; anywhere in your code, especially if xyz is std. I use the using std::vector; form, since that only pulls a single element from the namespace into pseudo-global scope, therefore leading to far less risk of a collision.

@Lightness Races in Orbit you are of course entitled to your opinion. Would have been more helpful if there had been some attempt at explanation why you do not agree with advice given in this answer. Especially would be interesting to understand what is the point of namespaces if 'using' them is bad? Why not just name things std_cout instead of std::cout ... the creators of C++/namespace must have had some idea when they bothered to create them.

@nyholku: No need - the majority of the other answers give the same reasons I would. Also please do no hesitate to note the ":)" I appended to my comment! And that I didn't say namespaces are bad.

Yeah, I noticed that :) but IMO the majority of answer (that go against this sage advice) are misguided (not that I made any statistics what is the majority now). If you agree that namespace are 'not bad' then you might say where you think they are appropriate if you disagree with this answer?

c++ - Why is "using namespace std" considered bad practice? - Stack Ov...

c++ namespaces std c++-faq