-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Do not define an unnamed namespace in a header file (DCL59-CPP) #552
Comments
That explanation is odd. It acknowledges that it uses internal linkage but then cautions that it could trigger ODR violation -- which directly contradicts what internal linkage means. |
Hi, the rationale can be found in this paper
|
Here is the complete explanation from the guidelines: |
@theodelrieu If I understand you correctly, then I can mark this issue as "won't fix", or rather a "false positive" for the underlying rule? |
Yep, in that case we really want an unnamed namespace, "won't fix" seems appropriate |
This is a bit of a necro, but someone sent me this link as it was relevant to a discussion we were having, and in fact what's happening here is 100% safe. The major concern is basically that that if a user defines an inline function in a header file, and that inline function uses |
Thanks, your explanation is more digest than my copy-paste :) |
@theodelrieu haha well your copy-paste is quite clear on the internal linkage aspect. The key point though is that even if you get internal linkage for the reference, this can still be very bad, @jaredgrubb is only superficially correct: yes, internal linkage means that it will not directly trigger ODR violations and UB, but unless you plan to ban users from using the variable in question from their inline functions (which is a strange and unenforceable restriction), it's a very easily open door to UB. In short, you only want to use internal linkage, header file globals for things that are both constant and constant expression initialized. |
The SEI CERT C++ Coding Standard states in DCL59-CPP:
The code currently has one unnamed namespace:
I am not sure whether the above problem exists. Maybe @theodelrieu has an idea why we need that namespace in the first place.
The text was updated successfully, but these errors were encountered: