Best way to scope?

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!" << std::endl;
    ::std::cin.ignore();
}
Which style of scoping do you think is the better style to use in various situations?

I would also like to know why when using namesoace std; one can still use std::endl even though there is no std namespace in the std namepsace? What if there was?

Personally I prefer the second without the using namespace std;, but the third looks anti-scope-related-error proof...
According to the C++ Coding Standards by Sutter and Alexandrescu,

in .h files:

"In header files, don't write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names."

in .cc files:

"Never write a using declaration or a using directive before an #include directive. [...] You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it."

As for the fully-qualified form (::std::cin), I think it's excessively pedantic, but I did see it used.

PS: when using namespace std; you're not deleting namespace std. It's still there, with all of its names, so std::cin and ::std::cin both work as before. But you're also injecting every name from std into the global namespace, so cin and ::cin now work as well.
Last edited on
Topic archived. No new replies allowed.