Overloading standard math functions for custom types

Suppose I write a class representing a mathematical object for which it makes sense to define various standard operations.

Overloading arithmetic operators, I enjoy the fact that any template algorithm that uses them will work, as expected, when defining my class. But what if the algorithm uses something like std :: pow?

The standard states that only specialized function templates are allowed in the std namespace, which means that I am not allowed to write my own std :: pow overload for my class. But if this is true, then what is the best approach to ensuring pedigree yet?

+4
source share
1 answer

Comment Yakka is the correct answer. You put powyours in your namespace, just like a standard puts it powin std::. The dependent search argument means what it pow(x,y)will look like in the type namespaces xand y.

Experienced library developers know how to use ADL effectively, but TBH I would not immediately recognize a library that has good reason for calling powobjects of an unknown type.

+3
source

Source: https://habr.com/ru/post/1523817/


All Articles