llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.43k stars 12.16k forks source link

Should abs be in the flat namespace? #15246

Closed llvmbot closed 11 years ago

llvmbot commented 11 years ago
Bugzilla Link 14874
Resolution INVALID
Resolved on Jan 09, 2013 10:16
Version unspecified
OS MacOS X
Reporter LLVM Bugzilla Contributor

Extended Description

I'm trying to compile a library with clang and libc++, and stumble upon many name conflicts as this library has redefined functions named "abs", "norm", etc... in the global namespace.

This library (named Freefem++) compiles well with libstdc++; I had a quick look and I am under the impress that the difference is in cmath header file: in libstdc++ the abs function is in std namespace whereas in libc++ it is in global one.

Am I right in my assessment, and if so which library complies with the C++ standard on that point?

Best regards,

Sebastien Gilles

llvmbot commented 11 years ago

My mistake; I missed the macro preprocessor that declares the namespace (namely _LIBCPP_BEGIN_NAMESPACE_STD); I was comforted in y wrong assessment by some std:: in the file but they were all before the macro)

Thanks a lot for your quick reply,

Best regards,

Sebastien

llvmbot commented 11 years ago

I just preprocessed for both libstdc++ and libc++.

libstdc++ produces:

namespace std {

inline double abs(double x) { return builtin_fabs(__x); }

inline float abs(float x) { return builtin_fabsf(__x); }

inline long double abs(long double x) { return builtin_fabsl(__x); }

}

libc++ produces:

namespace std {inline namespace __1 {

inline attribute ((visibility("hidden"), __always_inline)) float abs(float x) throw() {return fabsf(__x);}

inline attribute ((visibility("hidden"), __always_inline)) double abs(double x) throw() {return fabs(__x);}

inline attribute ((visibility("hidden"), __always_inline)) long double abs(long double x) throw() {return fabsl(__x);}

} }

Neither declares abs in the global namespace. Both libraries refuse to compile this program:

include

int main() { abs(1.0); }

test.cpp:5:5: error: use of undeclared identifier 'abs'; did you mean 'fabs'? abs(1.0); ^~~ fabs