3

So for a while I've been using...

using namespace::std;

and recently I realized that's supposed to be

using namespace std;

Can somebody explain to me why what I was doing worked, and how it differs from the correct way of declaring the usage of a certain namespace?

2
  • 3
    Please consider not doing this at all. Commented Jun 21, 2013 at 16:46
  • 2
    dont't use either. Commented Jun 21, 2013 at 16:46

1 Answer 1

6

The first version works because the compiler sees it as

using namespace ::std;
// Notice space^

That is simply telling the compiler to look for the name-space std in the global scope.

The scope operator :: without anything on the left-hand side is the same as the global scope.

Sign up to request clarification or add additional context in comments.

5 Comments

so it is not the same as std we regularly mean?
@taocp it is the same, since std is in the global namespace.
@taocp: It is the same. ::std and std are equivelant in this context.
@taocp It is, since std is in the global scope, using std or ::std is the same.
Thank you @juanchopanza,John Dibling, Joachim Pileborg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.