0

I have the below code

#include<iostream>
namespace first{
    void test()
    {
        std::cout<<"This is the first one \n";
    }
}
namespace second{
    void test()
    {
        std::cout<<"This is the second one \n ";
    }
}
int main()
{
   using namespace first;
   test();
   using namespace second;
   test();
   return 0;
}

The above code gives me compilation error of calling of overloaded function test. My doubt why cannot the second using statement using namespace second hides the first namespace?

Next I tried declaring the namespace inside a function body, this may sound strange but i tried to check whether i can do that or not, It also resulted in the compilation error. Which makes me wonder is it only at the global scope that I can declare the namespaces?

My third and last question is, is there any way by which I can only declare the two function with the same signature inside two different namespaces and the define them outside the outside the namespace block? Like below

#include<iostream>

namespace first{
    void test();
}

namespace second{
    void test();
}

int main()
{
    return 0;
}

Again idk whether my question is valid or not.

1
  • @DevSolar I have edited the question Now Commented Sep 16, 2015 at 7:25

2 Answers 2

1

I assume both of the using statements precede the call of test() (as distinct from one before and one after, as you have presented it) since, otherwise, the code is fine.

Namespaces do not hide each other. using namespace first makes the identifiers (types, variable, function names, etc) in first become candidates for matching in subsequent code.

By preceding the call of test() with

using namespace first;
using namespace second;

the effect is that both first::test() and second::test() become candidates for matching a subsequent unqualified usage of test(). The compiler has no reason to prefer one over the other - they are both equally valid matches - so the compiler rejects the code due to ambiguity over which one to select.

Also, test() is not overloaded in your example. Two distinct functions in different namespaces are declared and defined. Their full names (like first::test() and second::test() are distinct.

To answer your second question, namespaces can be declared within namespaces.

For your third question, defining the functions separately from their declaration, is possible;

namespace first
{
    void test();
}

void first::test()   
{
    std::cout<<"This is the first one \n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes agreed that namespace can be declared under a namespace called nesting of the namespace but Why the namespace cannot be declared inside a function body. I might not achieve anything good by doing that but I just want to know the reason for that
@RohitSaluja: Because the standard committee doesn't add features "because they can". They add features when somebody shows them that feature would be useful, compatible, and worth the effort of requiring compiler maintainers to implement it. Apparently no-one has made such a case for namespaces declared inside a function body. (And why would you, what you're declaring inside a function is local to the function anyway, so why namespaces?)
A namespace introduces a scope in which it is guaranteed that all identifiers declared within it are unique (unless within nested scopes). A compound statement (started with a { and ended with a }) already has exactly that property, as does a function body. Permitting a namespace within a function (or within a compound statement) is therefore unnecessary. There is nothing such a language feature would achieve, that can't already be achieved more easily, so there is no value in supporting it.
0

The error you get is not because of an overload, but because of ambiguity of test(): The compiler "sees" two definitions, first::test() and second::test(), with no hint which one to use.

using namespace second; adds the identifiers from second:: to the global namespace; it does not hide any that are already global (either defined globally or previously introduced via using namespace). That's just the way it works.

Also, namespaces can be declared either at global scope, or inside other namespaces; they cannot be declared inside function bodies (as you found out).

That being said, "trial and error" (what does a given compiler accept or not) is a bad habit. What you're learning is not "the language", but a specific implementation. The difference will come back to haunt you if your code is one day expected to compile on a different implementation, or run on a different platform.

In order to resolve the ambiguity, you can always use the fully qualified names, first::test() and second::test(), both when defining and when calling the functions.

Comments