Why is using namespace std considered bad practice

0 votes
Others have warned me that utilizing the namespace std; in code is incorrect, and that I should instead use std::cout and std::cin directly.

Why is it considered poor practice to use the namespace std;?

Is it inefficient, or does it risk declaring ambiguous variables (variables with the same name as a std namespace function)?

Does it have an effect on performance?
Jun 1, 2022 in C++ by Nicholas
• 7,760 points
810 views

1 answer to this question.

0 votes

This has nothing to do with performance. 

However, imagine this: you're utilizing two libraries, Foo and Bar:

using namespace foo; 

 using namespace bar;

Everything is good, and you can easily use Blah() from Foo and Quux() from Bar. 

However, one day you upgrade to Foo 2.0, which now includes a feature named Quux (). 

You've created a conflict: 

Quux() is imported into your global namespace by both Foo 2.0 and Bar. 

Fixing this will take some time, especially if the function parameters are same.

The introduction of foo::Quux() would have been a non-event if you had used foo::Blah() and bar::Quux().

answered Jun 1, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Is C++ considered weakly typed? Why?

C++ has always struck me as one ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
1,553 views
0 votes
1 answer

What is the difference between std::__gcd and std::gcd?

I done some research about this. The ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,687 views
0 votes
1 answer

str_lib_facilities.h file from Stroustrup's Programming: Principles and Practice Using C++ generating errors

Switching the compiler to compile in c++11 ...READ MORE

answered Jun 13, 2022 in C++ by Damon
• 4,960 points
984 views
0 votes
1 answer

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
644 views
0 votes
1 answer

How to expose std::pair to python using boost::python?

The most simple example of exposing std::pair is: class_<std::pair<int, int> ...READ MORE

answered Jun 20, 2019 in Python by SDeb
• 13,300 points
1,232 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
13,759 views
0 votes
1 answer

What is this weird colon-member (" : ") syntax in the constructor?

Foo(int num): bar(num) In C++, this is known as a Member Initializer List. Simply put, it sets the value of your member bar to num. There is a significant difference between initializing a member with the Member initializer list and assigning a value to it within the function Object() { [native code] } body. When you use the Member initializer list to initialise fields, the constructors are only called once, and the object is constructed and initialised in a single operation. If you use assignment, the fields will be initialised with default constructors and then reassigned with actual values (via the assignment operator). As you can see, there is an extra overhead of creation and assignment in the latter, which may be significant for user defined classes. Cost of Member Initialization =Object ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
1,461 views
0 votes
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

answered Jun 6, 2022 in C++ by pranav
• 2,590 points
825 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
810 views
0 votes
1 answer

C++ auto keyword. Why is it magic?

Auto was a keyword that C++ "inherited" from C and had been around for a long time but was almost never used because there were only two possibilities: it wasn't allowed or it was assumed by default. C++11 introduced the usage of auto to denote an inferred type. Similarly to how template type deduction works for function templates, auto x = initializer deduces the type of x from the type of initializer.  Consider the following function template: template<class T> int whatever(T t) { ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
631 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP