> On May 12, 2016, at 7:00 AM, 军其 胡 <junq...@me.com> wrote:
> Hi, all.
> 
> My question is about the convention of the standard namespace in C++. 
> firstly, let's read the following classic example in C/C++:
> 
> ,-----------------------------
> | #include <cstdio>
> | #include <cmath>
> |
> | double fabs(double a)
> | {
> |     return -1.0*a;
> | }
> |
> | int main()
> | {
> |     double b=fabs(3.0);
> |     double c=std::fabs(3.0);
> |     printf("%f, %f\n",b,c);
> | }
> `-----------------------------
> 
> The Xcode (or Apple Clang) gives the results with both 3.000000.  I have 
> tried to compile the same code by GCC on Fedora 22 and obtain the opposite 
> results with both -3.000000. It's ridiculous since fabs and std::fabs are 
> lying in different namespaces.

<cmath> is allowed to declare entities in both namespace std and the global 
namespace, and they are permitted to be the same entity.  As far as the 
compiler is concerned, your declaration of fabs is an attempt to implement the 
C standard library's fabs function, which the C++'s library is just an alias 
of.  That is why both of these calls call your function.

You should not declare functions in the global namespace with the same name as 
C library functions unless you really do mean to override them.

John.

> I also compile the following C/C++ codes:
> 
> ,-----------------------
> | #include <cstdio>
> | #include <cmath>
> |
> | int abs(int a)
> | {
> |     return -1*a;
> | }
> |
> | int main()
> | {
> |     int b=abs(3);
> |     int c=std::abs(3);
> |     printf("%b, %c\n", b, c);
> | }
> `-----------------------
> 
> in Xcode. Now, it gives me the error about ambiguous calling to std::abs 
> function while GCC gives me the right answer. After browsing the hierarchy of 
> C/C++ headers
> 
> ,------------------------------------------------------------------------------------------------------------------
> | $ cd /Applications/Xcode.app/Contents
> | $ find . -iname 'cmath'
> | 
> ./Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/c++/4.2.1/cmath
> | 
> ./Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/c++/4.2.1/tr1/cmath
> | 
> ./Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/c++/4.2.1/cmath
> | 
> ./Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/c++/4.2.1/tr1/cmath
> | 
> ./Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1/cmath
> | 
> ./Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1/tr1/cmath
> | ./Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath
> `------------------------------------------------------------------------------------------------------------------
> 
> I found out that Apple Clang doesn't offer the newest math library. Anyone 
> knows what happened between math library and standard namespace?
> 
> 
> Best wishes,
> 
> Jun-Qi Hu
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/rjmccall%40apple.com
> 
> This email sent to rjmcc...@apple.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to