-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?

3
  • 1
    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. Commented Feb 28, 2018 at 10:28
  • Thanks, I'll keep that in mind for the future. Commented Feb 28, 2018 at 10:32
  • Please also see Why is "using namespace std" considered bad practice? Commented Feb 28, 2018 at 12:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@ShiwangGupta the compiler lookup time for symbols is absolutely a non-issue.
@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.
-1

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.

2 Comments

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.
... which is to say, this currently is a correct answer, but not yet a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.