// main.cpp
const double MAX = 3.5;
namespace ns
{
const int MAX = 3;
}
int main()
{
}
will this cause redefinition error?
I’m referring to this MSDN page, which says in the Remarks section that this is an error.
Update: I think I may miss one important statement when copying around the code.
using ns::MAX;
No – I don’t see how that code would cause a redefinition error.
And in fact, you can compile it and see for yourself.
EDIT: Following up now that you’ve supplied the link to the MSDN page you mentioned…
That MSDN page is talking about name clashes in the context of a
usingdirective:Here’s an example of a local variable hiding a namespace variable that’s been brought into scope by a
usingdirective:The inclusion of the
usingdirective brings all of the names declared within thensnamespace into scope. However, when I assign the value ofMAXtotest, it’s the local variableMAXthat is used in the assignment because the namespace variableMAXis hidden. The local variable takes precedence and hides the namespace variable.Now here’s an example of a clash between a namespace variable and a global variable.
Consider this amended piece of code (and you can see it compile here):
Again, the
usingdirective bringsns:MAXinto scope, and the global variableMAXis also in scope.When I go to use the variable called
MAXinmain(), the compiler reports an error because the nameMAXis now ambiguous: it has no way of knowing whichMAXwe are referring to, as there are two non-localMAXs to choose from: neither has any precedence.