Ambiguous call to abs

后端 未结 3 1063
执念已碎
执念已碎 2021-01-11 18:15

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 templa

3条回答
  •  遥遥无期
    2021-01-11 18:23

    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.

提交回复
热议问题