CFType functions prevent same-named C++ namespace creation

Originator:mtedder
Number:rdar://26262112 Date Originated:13-May-2016 01:47 PM
Status:Open Resolved:
Product:OS X SDK Product Version:10.11
Classification:Other Bug Reproducible:Always
 
Summary:
The CFType functions declared in Core Foundation are exported to the global namespace and can cause a name collision with an unfortunately same-named C++ namespace.

For example, the following code fails to compile, resulting with an error of "redefinition of 'CFHash' as different kind of symbol":

//--- begin code snippet ---
#import <CoreFoundation/CoreFoundation.h>
namespace CFHash {
	class SHA512 {
	};
}
//--- end code snippet ---

Given that renaming the CFType functions would be impossible without API breakage, is there a possibility to add a #define to control renaming of these function calls to a less common name, or even completely skipping their declarations?

Perhaps something similar to the __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES found in AssertMacros.h would be helpful.

Steps to Reproduce:


Expected Results:
The above code snippet compiles without a compile error.

Actual Results:
clang errors with:

/Users/falken/src/ColdFusion/tests/main.cpp:2:11: error: redefinition of 'CFHash' as different kind of symbol
namespace CFHash {
          ^
In file included from /Users/falken/src/ColdFusion/tests/main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:38:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h:630:12: note: previous definition is here
CFHashCode CFHash(CFTypeRef cf);
           ^
1 error generated.


Version:
Xcode 7.3.1 (7D1014), OS X 10.11 SDK, OS X 10.11.4

Notes:
Since the CFHash() function does not conflict with a C++ namespace at link time, one workaround is to use a preprocessor #define to rename the CFHash() function to something else, then include the header.

For example, the following code compiles without issue:

#define CFHash __Apple_CFHash
#import <CoreFoundation/CoreFoundation.h>
#undef CFHash
namespace CFHash {
	class Test {
	};
}


Configuration:


Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!