我有一个自定义数据类型,实际上它可以是float或double.在除OSX之外的所有OS上,我都能成功构建此C ++ 11模板:
#include <cmath> #include <cstdlib> #include <cstdint> template< class REAL_T > inline REAL_T inhouse_abs(REAL_T i_val) { return std::abs((REAL_T)i_val); } int main() { int32_t ui = 2; inhouse_abs(ui); return 0; }但是,clang 6.0(3.5 LLVM)报告了一个模糊的函数调用.如果将abs更改为fabs,该错误将在OSX上得到解决,但是现在在我的Linux clang,gcc和Visual Studio中会出现相同的错误.
带有fabs的Visual Studio错误:
349 error C2668: 'fabs' : ambiguous call to overloaded function更新
此示例在我们的OS X系统上编译,尽管在几乎相同的项目中却没有.解决方案是在源中显式包括<cstdlib>,而不是在另一个标头中.原因尚不清楚,但似乎是xcode/clang没有正确遵循我们的标头包含的内容.
解决方案问题在于,libc++并不完全与C ++ 11兼容, std :: abath in cmath :
double fabs( Integral arg ); (7) (since C++11)包含 cstdlib 可解决您的问题,因为该标头具有专门用于整数类型.
作为参考,C ++ 11标准草案草案26.8 [c.math] 段落11表示:
此外,还应该有足够的其他重载来确保:
并包括以下项目:
由于 LWG活动问题2192:std :: abs(0u)的有效性和返回类型不清楚.由于此缺陷报告中提到的问题,我猜libc++选择不提供cmath中的重载.
有关更多详细信息,请参见 std :: abs(0u)是否格式错误?.
I have a custom data type that in practice can be either float or double. On every OS except OSX, I am able to successfully build this C++11 template:
#include <cmath> #include <cstdlib> #include <cstdint> template< class REAL_T > inline REAL_T inhouse_abs(REAL_T i_val) { return std::abs((REAL_T)i_val); } int main() { int32_t ui = 2; inhouse_abs(ui); return 0; }However, clang 6.0 (3.5 LLVM) reports an ambiguous function call. If I change abs to fabs, the error is resolved on OSX, but now an identical error shows up on my Linux clang, gcc, and Visual Studio.
Error on Visual Studio with fabs:
349 error C2668: 'fabs' : ambiguous call to overloaded functionUPDATE
This example compiled on our OS X systems, although in the nearly identical project it does not. The solution was including <cstdlib> explicitly in the source, rather than back in another header. The reason is unclear, but seems to be xcode/clang not following our header includes properly.
解决方案The issue is that libc++ is not entirely C++11 compliant with the integral overload for std::abs in cmath:
double fabs( Integral arg ); (7) (since C++11)Including cstdlib solves your problem since that header has overloads specifically for integer types.
For reference the draft C++11 standard section 26.8 [c.math] paragraph 11 says:
Moreover, there shall be additional overloads sufficient to ensure:
and includes the following item:
This is situation very likely to change due to LWG active issue 2192: Validity and return type of std::abs(0u) is unclear. I am guessing libc++ choose not to provide the overloads in cmath due to the issue brought up in this defect report.
See Is std::abs(0u) ill-formed? for more details on this.
更多推荐
含糊不清的Abs通话
发布评论