C++ 为自定义(模板)类重载 std::abs 吗?

mez*_*hic 1 c++ c++20

写了我自己的数字类。超载std::numeric_limit::max()就好。然而,我正在尝试超载std::abs()但编译器不喜欢我的尝试。我试过这个:

\n
namespace std \n{\n    template<uint8_t T> \n    MyType<T> abs(const MyType<T>& mt)\n    {\n        MyType<T> copy = mt;\n        copy.val = std::abs(md.val);\n        return copy;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但是,编译器没有发现重载:

\n
error: no matching function for call to \xe2\x80\x98abs(const MyType<10>&)\xe2\x80\x99\n/usr/include/c++/11/bits/std_abs.h:56:3: note: candidate: \xe2\x80\x98long int std::abs(long int)\xe2\x80\x99\n   56 |   abs(long __i) { return __builtin_labs(__i); }\n      |   ^~~\n/usr/include/c++/11/bits/std_abs.h:56:12: note:   no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98long int\xe2\x80\x99\n   56 |   abs(long __i) { return __builtin_labs(__i); }\n      |       ~~~~~^~~\n/usr/include/c++/11/bits/std_abs.h:61:3: note: candidate: \xe2\x80\x98long long int std::abs(long long int)\xe2\x80\x99\n   61 |   abs(long long __x) { return __builtin_llabs (__x); }\n      |   ^~~\n/usr/include/c++/11/bits/std_abs.h:61:17: note:   no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98long long int\xe2\x80\x99\n   61 |   abs(long long __x) { return __builtin_llabs (__x); }\n      |       ~~~~~~~~~~^~~\n/usr/include/c++/11/bits/std_abs.h:71:3: note: candidate: \xe2\x80\x98constexpr double std::abs(double)\xe2\x80\x99\n   71 |   abs(double __x)\n      |   ^~~\n/usr/include/c++/11/bits/std_abs.h:71:14: note:   no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98double\xe2\x80\x99\n   71 |   abs(double __x)\n      |       ~~~~~~~^~~\n/usr/include/c++/11/bits/std_abs.h:75:3: note: candidate: \xe2\x80\x98constexpr float std::abs(float)\xe2\x80\x99\n   75 |   abs(float __x)\n      |   ^~~\n/usr/include/c++/11/bits/std_abs.h:75:13: note:   no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98float\xe2\x80\x99\n   75 |   abs(float __x)\n      |       ~~~~~~^~~\n/usr/include/c++/11/bits/std_abs.h:79:3: note: candidate: \xe2\x80\x98constexpr long double std::abs(long double)\xe2\x80\x99\n   79 |   abs(long double __x)\n      |   ^~~\n/usr/include/c++/11/bits/std_abs.h:79:19: note:   no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98long double\xe2\x80\x99\n   79 |   abs(long double __x)\n
Run Code Online (Sandbox Code Playgroud)\n

调用它的代码:

\n
MyType<10> mt;\nMyType<10> abs = std::abs(mt);\n
Run Code Online (Sandbox Code Playgroud)\n

Nat*_*ica 6

不允许您向std命名空间添加重载。在 C++20 之前,您可以对函数进行专门化,但这不会有任何帮助,因为您的类型是模板化的,并且不允许函数的部分模板专门化。

通常,您所做的就是在代码中导入要使用的函数,然后对所述函数进行无限定调用,例如

template <typename T>
auto some_func(const T& foo)
{
    using std::abs;
    return abs(foo);
}
Run Code Online (Sandbox Code Playgroud)

现在如果T是一个类型std::abs可以处理那么它将被使用。如果不是,但它是一个具有自己的ABS函数的类型,或者在全局空间中声明,或者在该类型自己的命名空间中声明,那么将使用非限定查找规则找到它。


归档时间:

查看次数:

519 次

最近记录:

3 年,1 月 前