CVS
CVS

Reputation: 73

Using namespace scope

I tried the following piece of code. When i compile, i get error that there there are ambiguous instances of first_var, whereas i have introduced using namespace second_space before the last cout

I guess this is because the last cout is using both the namespaces. There is no override concept for namespaces ? Is there anyway a namespace scope can be ended or it continues from the using namespace point to the end of file?

#include<iostream.h>
namespace first_space{
    int first_var;
}
namespace second_space{
    int first_var = 1;
}
int main()
{
    cout<<"Hello World"<<endl;
    cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
    using namespace first_space;
    cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
    using namespace second_space;
    cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}

Edit 1:

I tried something like this below. Declared a variable with same name inside the main, assigned a value 1 to it and then used using namespace below that. But I see that, the value of first_var is printed as 1 in the last two cout. There is no ambiguity here. So the namespace didn't have any effect? Why is it so?

#include<iostream.h>
namespace first_space{
    int first_var;
}
namespace second_space{
    int first_var = 1;
}
int main()
{
    int first_var =1 ;
    using namespace first_space;
    cout<<"Hello World"<<endl;
    cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
    cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
 //   using namespace second_space;
    cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}

Output:

Hello World
First Namespace Variable using namespace identifier:0
First Namespace Variable using using identifier:1
Second Namespace Variable using using identifier:1

Upvotes: 3

Views: 1671

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117866

Yes you are correct, after the 2nd using statement, the variable first_var is now ambiguous because both of the two namespaces are valid and of equal precedence as far as name lookup is concerned.

The two workarounds are

a) add braces to enforce an anonymous scope (live demo)

{
using namespace first_space;
cout << "First Namespace Variable using using identifier:" << first_var << endl;
}

{
using namespace second_space;
cout << "Second Namespace Variable using using identifier:" << first_var << endl;
}

b) drop the using keyword and use the namespace scope directly

cout << "First Namespace Variable using using identifier:" << first_space::first_var << endl;
cout << "Second Namespace Variable using using identifier:" << second_space::first_var << endl;

Personally I would go with option b. One of the main reasons you add namespaces in the first place is to avoid ambiguity issues, so polluting the current scope with a bunch of using statements undermines that.

Upvotes: 5

Related Questions