Off the top of my head, I only remember LLVM's standard library do this, but I remember seeing other libraries also do this. I'm not entirely sure what is the advantage here. I feel like namespace std { would be clearer than _LIBCPP_BEGIN_NAMESPACE_STD?

7 Replies 7

Compatibility with compilers that don’t support namespaces.

It's useful for compatibility, testing, etc. For example you may want to make use of inline namespaces but also support compilers that don't have that feature, yet, so you make it possible to disable it via the preprocessor.

LLVM itself also adds some other macros on top of the namespace declaration, making the macro shorter and avoiding repetition. See its definition in this file

Qt optionally uses a namespace to avoid linker errors in certain configurations. If Qt started today, they'd probably always use a namespace (and some inline namespaces on top) but it's a pretty old library with high regards for backwards compatibility, so they can't change now.

Gives you a chance to change the namespace in the remote possibility of a clash. I use BLAHBLAH_NAMESPACE_BEGIN and BLAHBLAH_NAMESPACE_END, I like the readability.

It's for ABI versioning. See the definitions of those macros.

If for some reason you need to use two versions of the same library in a single binary, you can change the namespace of one copy

One reason may be C compatibility or the programmer is coming from C background.

BTW, don't look to standard library implementations for code that can be used in reasonable user programs. Remember that the standard library may make non-portable assumptions about the compiler it's used with, and is allowed to use otherwise-reserved identifiers (such as the macro name mentioned in this question).

Your Reply

By clicking “Post Your Reply”, 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.