启用swapSTL算法的正确方法是什么?
1)会员swap.是否std::swap使用SFINAE技巧来使用该成员swap.
2)自由站立swap在同一名称空间中.
3)部分专业化std::swap.
4)以上所有.
谢谢.
编辑:看起来我没有清楚地说出我的问题.基本上,我有一个模板类,我需要STL algos来使用我为该类编写的(高效)交换方法.
写了我自己的数字类。超载std::numeric_limit::max()就好。然而,我正在尝试超载std::abs()但编译器不喜欢我的尝试。我试过这个:
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}\nRun Code Online (Sandbox Code Playgroud)\n但是,编译器没有发现重载:
\nerror: 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: …Run Code Online (Sandbox Code Playgroud)