-
what everyone seems to be failing to realize is that even when a "using namespace std" declaration is employed in the global scope, a member of a child scope may still be called by any of the names defined in the global scope. child scope names have precedence over parent scope names. take the following code for example:
Code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
char* string = nullptr;
return 0;
}
the variable called "string" in main() does not conflict with std::string, because the C++ compiler takes any unqualified identifier to mean the one defined in the nearest scope.
even the following code is valid:
Code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
typedef char* string;
string s = nullptr;
return 0;
}
I have confirmed this behavior on both G++ 4.5.1 and VC++ 2010.
-
That's true even if using namespace std is in the child scope.
And who's "everybody"? your example is virtually identical to the one I linked to.
For more confusion, try this:
Code:
#include <string>
#include <iostream>
int main()
{
using namespace std;
string string;
string = "hello\n";
cout << string;
}