Ambiguous call to abs

后端 未结 3 1036
执念已碎
执念已碎 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:29

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

    #include 
    #include 
    #include 
    
    namespace util {
    
    
    template 
    auto abs(T value) -> std::enable_if_t::value,
                                          T> { return value; }
    template 
    auto abs(T value) -> std::enable_if_t::value,
                                          T> { return std::fabs(value); }
    template 
    auto abs(T value) -> std::enable_if_t::value,
                                          T> { return std::abs(value); }
    template 
    auto abs(T value) -> std::enable_if_t::value,
                                          T> { return std::labs(value); }
    template 
    auto abs(T value) -> std::enable_if_t::value,
                                          T> { return std::llabs(value); }
    template 
    auto abs(T value) -> std::enable_if_t::value &&
                                              !std::is_floating_point::value &&
                                              !std::is_same::value &&
                                              !std::is_same::value &&
                                              !std::is_same::value,
                                          T> { return std::abs(value); }
    
    
    } // namespace util
    

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

提交回复
热议问题