Ambiguous call to abs

≯℡__Kan透↙ 提交于 2019-12-01 03:23:46
Shafik Yaghmour

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.

The solution was to explicitly #include <cstdlib> on the OS X machine, as for some reason Visual Studio finds it in our dependencies and includes it, but clang does not. I will try to reproduce a similar chain of includes that our project has and reproduce the error in a minimalist way, as it may still be a problem either with Xcode or Visual Studio.

If you have many template functions causing this problem, you can use the following drop-in replacement:

#include <cmath>
#include <cstdlib>
#include <type_traits>

namespace util {


template <class T>
auto abs(T value) -> std::enable_if_t<std::is_unsigned<T>::value,
                                      T> { return value; }
template <class T>
auto abs(T value) -> std::enable_if_t<std::is_floating_point<T>::value,
                                      T> { return std::fabs(value); }
template <class T>
auto abs(T value) -> std::enable_if_t<std::is_same<T, int>::value,
                                      T> { return std::abs(value); }
template <class T>
auto abs(T value) -> std::enable_if_t<std::is_same<T, long>::value,
                                      T> { return std::labs(value); }
template <class T>
auto abs(T value) -> std::enable_if_t<std::is_same<T, long long>::value,
                                      T> { return std::llabs(value); }
template <class T>
auto abs(T value) -> std::enable_if_t<std::is_signed<T>::value &&
                                          !std::is_floating_point<T>::value &&
                                          !std::is_same<T, int>::value &&
                                          !std::is_same<T, long>::value &&
                                          !std::is_same<T, long long>::value,
                                      T> { return std::abs(value); }


} // namespace util

Just replace the std::abs calls with util::abs. (Needs c++11.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!