含糊不清的Abs通话 [英] Ambiguous call to abs

查看:129
本文介绍了含糊不清的Abs通话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义数据类型,实际上它可以是floatdouble.在除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兼容, cstdlib 可解决您的问题,因为该标头具有专门用于整数类型.

作为参考,C ++ 11标准草案草案26.8 [c.math] 段落11表示:

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

并包括以下项目:

  1. 否则,如果与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:

  1. 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通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆