Skip to content

Code guidelines: anonymous namespace - enhancement #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2019

Conversation

GuyWi
Copy link
Contributor

@GuyWi GuyWi commented Mar 27, 2019

This PR addresses https://jira.arm.com/browse/IOTDOCS-869, and is related to ARMmbed/mbed-os#9158 and issue #884.

@@ -30,6 +30,7 @@ The Arm Mbed OS codebase is organized into conceptual submodules to limit the sc
- As an entry point for the module (from the user space), we suggest a single header file. For example: `mbed.h`, `rtos.h`.
- Header files should limit external includes to avoid indirectly exposing unrelated APIs. Header files should not expand namespaces.
- In C++ modules, the API should be contained in a namespace that matches the module’s name. For example: `mbed::Ticker`, `rtos::Thread`, `netsocket::Socket`.
- Wrap the content of each C++ module inside an anonymous namespace. The `GCC_ARM` toolchain automatically gives data structures in different anonymous namespaces unique names. This prevents build failures caused by link time optimization (LTO) in the `GCC_ARM` toolchain when you have local data types with the same name in different files.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't wrap the whole module - it just needs to be the private stuff. And this isn't a GCC-specific thing. My suggestion:

Private class types defined in a C++source file should be contained in an anonymous namespace to ensure their names are globally unique. In C++ it is not permitted to have two different definitions of the same class name in different source files, even if they are not externally visible. In practice a name collision often doesn't cause a problem, but it can cause a build failure when link time optimization (LTO) is enabled, or a run-time failure if templates are instantiated using the private classes.

(I don't think we've seen the template problem, but it could happen - the compiler might instantiate Vector<foo> for two different foos in different files, and end up trying to share the same template instantiation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kjbracey-arm !
I have updated the PR based on your feedback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - I'm just a little nervous about the use of the word "private", as we're not talking about the C++ keyword private here. Maybe "internal"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again, @kjbracey-arm :)
Updated.

@GuyWi GuyWi requested a review from kjbracey March 27, 2019 09:30
Remove hyphen to fit our style guide.
Copy link
Contributor

@AnotherButler AnotherButler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR 👍

@GuyWi GuyWi merged commit 678e5a0 into development Mar 27, 2019
@GuyWi GuyWi deleted the Code-guidelines-anonymous-namespace---enhancement branch March 27, 2019 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants