unnamed-namespace

How would use of unnamed namespaces in headers cause ODR-violations?

那年仲夏 提交于 2020-01-31 11:47:20
问题 In the Google C++ Style Guide, the Namespaces section states that " Use of unnamed namespaces in header files can easily cause violations of the C++ One Definition Rule (ODR). " I understand why not using unnamed namespaces in an implementation file can cause ODR-violations, but not how use in a header can do so. How can this cause a violation? 回答1: The reason is that if you actually use anything in the anonymous namespace, you risk undefined behavior. For example: namespace { double const pi

Is static deprecated when ensuring availability between translation units?

旧时模样 提交于 2020-01-04 01:38:10
问题 From the following stackoverflow answer, the user says: It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the current C++ Standard - instead you are supposed to use anonymous namespaces: static int x = 0; should be: namespace { int x = 0; } I don't disagree that anonymous namespaces are the preferred method, but is using static really deprecated now? Where does

External linkage for name inside unnamed namespace

不问归期 提交于 2019-12-12 13:26:36
问题 According to the clause 3.5/4 of C++ Standard: An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. Simultanously in paragraph 7.3.1.1 we have note 96): Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit. How to explicitly make external linkage for name inside unnamed

How would use of unnamed namespaces in headers cause ODR-violations?

*爱你&永不变心* 提交于 2019-12-03 14:22:21
In the Google C++ Style Guide, the Namespaces section states that " Use of unnamed namespaces in header files can easily cause violations of the C++ One Definition Rule (ODR). " I understand why not using unnamed namespaces in an implementation file can cause ODR-violations, but not how use in a header can do so. How can this cause a violation? The reason is that if you actually use anything in the anonymous namespace, you risk undefined behavior. For example: namespace { double const pi = 3.14159; } inline double twoPiR( double r ) { return 2.0 * pi * r; } The rule for inline functions (and