user796530
user796530

Reputation:

Uniform Interface for Math functions for custom and standard type

I have some custom types a::Vector in a namespace a for which I have defined some math functions a::abs, a::pow and a::isnan among others. I want to be able to call abs(T t) without having to worry about whether the argument T is a double or a a::Vector. Using just abs everywhere correctly uses a::abs for a::Vector using ADL, however defaults to using C-style abs(int t) function even for double arguments while I want it to use std::abs or fabs.

I do not want to use using std::abs declarations since this are considered bad in general. What are my options to have a uniform interface abs(T t) for all the different types?

Upvotes: 2

Views: 71

Answers (1)

Jesin
Jesin

Reputation: 1019

If template<class T> abs(T x) were defined in std, then you could specialize that template, but there is no such generic template for std::abs. Adding overloads to namespace std is undefined behavior. https://stackoverflow.com/a/14403772

I would recommend using std::abs;. There is generally nothing wrong with using specific functions; it's only using namespace that's bad.

Upvotes: 1

Related Questions