-4

I am trying to understand namespaces in C++. I read that there are two ways of accessing namespace variables and functions. First one is to write using :: and second one is by using using directive at the top and not writing it again and again. I realized that the first method is better as the second one may lead to conflicts.

But, I want to know how actually 2nd method is working. For Example, If I write using namespace std at the top, how does the compiler know for which functions it has to add std:: in the beginning and for which ones it has no to. If I have written a function in main, firstly it will check in my main file for the function and then it will check in the header files ( that I have declared at top of main file) for the function declaration. Now, according to my understanding the functions in std are declared inside namespaces. So, I will not find it if I search without using ::.

So, when will std:: will get add at the beginning of a function?

bolov
  • 72,283
  • 15
  • 145
  • 224
shiwang
  • 150
  • 1
  • 13
  • please be mindful of the infamous wall of text. Try to structure your thoughts. I have done the minimum required and split your wall of text in some paragraphs. – bolov Feb 28 '18 at 10:28
  • Thanks, I'll keep that in mind for the future. – shiwang Feb 28 '18 at 10:32
  • Please also see [Why is "using namespace std" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Bo Persson Feb 28 '18 at 12:45

2 Answers2

1

(This is simplified, but it's the general gist of it.)

When you write std::bar, the compiler doesn't look for something named "std::bar", it looks for something named "bar" inside the "std" namespace.

using namespace std; makes the compiler look up names in both the current namespace and in std, so it doesn't need to add "std::" anywhere in order to look for "std::bar" - it will be found by looking for "bar" inside std as well as in the current namespace.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • So, if I declare namespace along with a function, it will check only in that namespace. So, it will also reduce the compiler lookup time. – shiwang Feb 28 '18 at 10:10
  • @ShiwangGupta the compiler lookup time for symbols is absolutely a non-issue. – bolov Feb 28 '18 at 10:36
  • @shiwang The difference in lookup time is negligible, and probably of roughly the same magnitude as the time it takes to parse the namespace prefix. You won't need to worry about it in your entire life. – molbdnilo Feb 28 '18 at 12:04
0

Here's a link to a description of how this works: http://en.cppreference.com/w/cpp/language/unqualified_lookup. A more general overview starts here(http://en.cppreference.com/w/cpp/language/lookup) and shows you all the cases when you have qualified names vs unqualified names.

Note that name resolution in C++ is actually quite complex. Argument Dependant Lookup (ADL) http://en.cppreference.com/w/cpp/language/adl can also apply when looking up where the function declaration is.

Also the compiler may need to do overload resolution as there can be multiple functions but with differing numbers of arguments and these overloads may exist in different namespaces.

James Sharpe
  • 524
  • 2
  • 8
  • an answer should be self sufficient. I.e. it should still be a good answer even if the links become invalidated (which is a real possibility).The links should be extra material, not the main material of the answer. – bolov Feb 28 '18 at 10:41
  • ... which is to say, this currently is a correct answer, but not yet a good answer. – MSalters Feb 28 '18 at 13:22