0

Consider this

#include<headerfile1>
#include<headerfile2>
.
.
#include<headerfilen>
using namespace std;

when I write this(header files are all in standard library of C++),does std namespace of all header files come into picture?

Also, if there are two libraries h1 and h2 and both have same namespace x and in those namespaces have same function func(). How do I resolve this?

Harsh Pathak
  • 85
  • 1
  • 8
  • yes, what else can it be – artm Dec 13 '16 at 09:58
  • 7
    Spare yourself the future headaches and never do `using namespace std;` at file scope. – StoryTeller - Unslander Monica Dec 13 '16 at 09:58
  • all of the std:: classes and functions declared in those header files will be available in the global namespace. And I echo @StroyTeller's sentiment. If you do this, eventually pain will follow. Particularly if you start using libraries with similar names - e.g. boost – Richard Hodges Dec 13 '16 at 09:59
  • That's fine,but what if there is a function collision in headerfilei and headerfilej if we use (using namespace std;)? – Harsh Pathak Dec 13 '16 at 10:02
  • this is why you shouldn't do that. familiarize yourself with [this question](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1) for example – slawekwin Dec 13 '16 at 10:02
  • I went through the example shown and I got it. But what if there are two libraries h1 and h2 and both have same namespace x and in those namespaces have same function func(). How do I resolve this? – Harsh Pathak Dec 13 '16 at 10:26
  • @HarshPathak this is a completely different question now, I suggest you research that and if you cannot find your answer ask a separate question here – slawekwin Dec 13 '16 at 10:31
  • @slawekwin I made two files a and b having namespace x and inside x a function func(). When I included a and b and called func() written in another file, and compiled it, it said: redefinition of func() in b previously defined in a – Harsh Pathak Dec 13 '16 at 10:46
  • Inside a namespace there cannot be two functions with the same name AND the same exact signature. – k_kaz Dec 13 '16 at 11:23

1 Answers1

0

From cppreference:

A using-directive that names the inline namespace is implicitly inserted in the enclosing namespace (similar to the implicit using-directive for the unnamed namespace).

For instance, in the following example, using namespace std::string_literals makes the operator visible inside the scope:

{ // in C++14, std::literals and its member namespaces are inline
   using namespace std::string_literals; // makes visible operator""s 
                                         // from std::literals::string_literals
   auto str = "abc"s;
}

If you use the using directive outside a scope (for instance as in your example), pain will follow, as the commenters stated, especially if this file is a header file: this namespace will be implicitly imported to any file that includes this one. This is why the proper way of managing a big project is to create your own namespace and put all your custom objects in that new namespace. If you really must use a using directive, do so in the implementation file, never in the header, and ideally within scopes. This will help you avoid most conflicts, unless your namespace uses a common name:

namespace Matrix{ <--- Bad practice, Matrix will probably conflict with something
myStuff ...
}

namespace JohnsAwesomeMatrix236790{ <--- Namespace name is unique,unlikely to get conflicts.
myStuff ... 
}

Also, it is much safer to import the few functions that you use instead of the entire namespace:

using namespace std; // Imports the entire namespace!

using std::cout;     // Much better, we only import
using std::endl;     // the names of the two functions we use a lot

Any decently written library you include will make very careful use of the using directive (if at all), so you normally don't have to worry about this. However, if you use code written with less stringent standards (e.g., scientific code) this is something to look out for.

Nikos Kazazakis
  • 792
  • 5
  • 19