含糊不清的Abs通话

编程入门 行业动态 更新时间:2024-10-06 06:46:26
本文介绍了含糊不清的Abs通话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个自定义数据类型,实际上它可以是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表示:

此外,还应该有足够的其他重载来确保:

并包括以下项目:

  • 否则,如果与double参数相对应的任何参数的类型为double或整数类型,则与之相对应的所有参数 double参数有效地转换为double.
  • 由于 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 function

    UPDATE

    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:

  • Otherwise, if any argument corresponding to a double parameter has type double or an integer type, then all arguments corresponding to double parameters are effectively cast to double.
  • 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通话

    本文发布于:2023-11-26 10:11:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1633667.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不清   Abs

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!