20

When using a using namespace statement inside an anonymous namespace bring the namespace used in to the file scope? Eg:

namespace foo
{
    int f() { return 1; }
}
namespace
{
    using namespace foo;
}
int a()
{
    return f(); // Will this compile?
}
4
  • 7
    have you tried compiling it? Commented Jan 8, 2012 at 5:26
  • 1
    It would probably be less effort to actually try compiling this than to ask it as a question on StackOverflow! Well done! Commented Jan 8, 2012 at 5:37
  • 19
    Of course, do note that this is a C++ question. Just because code compiles in a particular compiler does not speak to whether it is allowed by the standard. Commented Jan 8, 2012 at 5:41
  • 8
    Also, for those of us who found this question it's easier than compiling. Commented Feb 6, 2018 at 16:41

3 Answers 3

18

According to 7.3.4 [namespace.udir] paragraph 4 a namespace directive is transitive:

For unqualified lookup nominates a second namespace that itself contains using-directives, the effect is as if the using-directives from the second namespace also appeared in the first.

... and according to 7.3.1.1 [namespace.unnamed] paragraph 1 there is kind of an implicit using directive for the unnamed namespace:

An unnamed-namespace-definition behaves as if it were replaced by

inline namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }

where inline appears if and only if it appears in the unnamed-namespace-definition, all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.

Thus, the answer is "yes, this is supposed to compile" (and it does with all C++ compilers I tried it with).

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

Comments

3

Yes.

This is because an anonymous namespace is automatically brought into the containing scope.

Comments

1

Yes, because, as Dietmar Kühl quoted, an anonymous namespace is replaced by its content.

However, you should pay attention that it is replaced exactly where it is declared (edit), so there is no "magic" in this. For example, this won't work:

namespace foo
{
    int f() { return 1; }
}

int a()
{
    return f(); // Will this compile?
}

namespace
{
    using namespace foo;
}

2 Comments

An anonymous namespace is a proper namespace. Its initial declaration is replaced with the text he quoted, including a using namespace directive, but its body is not replaced with anything.
I take issue with "an anonymous namespace is replaced by it's content." The declaration and definition of a namespace are exactly the same thing. The content doesn't participate in any replacement.

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.