Is this a namespace collusion?

Consider the example below:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

namespace ns1
{
    void func(void){ std::cout << "I am inside ns1" << std::endl; }
}

namespace ns2
{
    void func(void){ std::cout << "I am inside ns2" << std::endl; }
}

using ns1::func;
using ns2::func;

int main()
{
    func();// Error: call of overloaded func()

    return 0;
}



Can this be qualified as a "namespace collusion"?

Thanks for reading.
That is an example of name clashing.

Forget having those using directives, fully qualify the use of whichever function you want to use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

// stores all the combat classes, functions and variables
namespace Combat
{
   void Fire()
   {
      std::cout << "You fire your crossbow.\n";
   }
}

// stores all the exploration classes, functions and variables
namespace Exploration
{
   void Fire()
   {
      std::cout << "You light your torch.\n";
   }
}

int main()
{
   Combat::Fire();
   Exploration::Fire();
}
You fire your crossbow.
You light your torch.
Last edited on
Thank you @George for your reply, this example is just an extraction piece of noobs' question topic, I read about the side effect of using using namesapce std, and using std::cout; from various topics in the web, and I encounter a lot of terms that I never heard of, one of them is namespace collusion.

I am planing from yesterday on! to explicitly writing namespace like you do in your example.
But here I am looking whether my code is an example of what is called namespace collusion?
Last edited on
I understand wanting to see what breaks C++ when doing things that are not a good idea. I do that myself a lot. I learn from doing stupid things, banging my head on the table repeatedly makes sure the knowledge doesn't fall out.
Last edited on
banging my head on the table repeatedly makes sure the knowledge doesn't fall out.

Funny and good one.

I still would like if my code above is what it is called technically namespace collision, or just a naming collision, or naming clashing.

I mean have you ever hear about the phrase "namespace collision" or someone invented it, or word It
It's not 'namespace collision'. That term doesn't make too much sense. A namespace can be used several times. So it is rather namespace collision. See this article:

https://www.learncpp.com/cpp-tutorial/naming-collisions-and-an-introduction-to-namespaces/
The function names are colliding/clashing in the global namespace.

I think a "namespace collision" would be if you for example tried to use two libraries that happen to use the same namespace name.
Last edited on
https://heimduo.org/what-is-a-namespace-collision/

Geared more towards Java than C++, though it does mention namespaces in C++ are "similar" to those in Java.
Topic archived. No new replies allowed.