我正在寻找有关如何为自定义数据类型实现专门的“最大”(即最大)函数的建议,该函数也适用于 float
等标量类型。 .我正在编写的数据类型是一个 vector 的包装器(最终是四个 float 的 SIMD vector ,而不是 std::vector
),我想提供一个 max
函数比较两个 vector 并返回一个新 vector ,该 vector 是每个元素的最大值。这不同于 std::max
它使用比较运算符,但概念是相同的。
问题是我有一个名为 do_max(T x, T y)
的通用函数适用 max
到输入。我需要此函数对标量浮点输入(例如 do_max<float>(0.1f, 0.2f)
)和我的 vector 类(例如 do_max<MyVector>(v0, v1)
)起作用。
请注意,重载 MyVector 的比较运算符不是一个选项,因为我使用的是具有完全不同的 SIMD 内在函数的那些:它们为每个元素比较创建一个包含 1、0、-1 的整数 vector ,而不是返回 bool 结果.
我下面的代码无法编译,除非你注释掉 float f0 = ...
行:
// compile with: g++ -std=c++11 max.cc -o max
#include <algorithm>
#include <vector>
class MyVector {
public:
MyVector(float x0, float x1, float x2, float x3) : storage_ { x0, x1, x2, x3 } {};
friend MyVector max(MyVector lhs, const MyVector & rhs);
private:
std::vector<float> storage_;
};
MyVector max(MyVector lhs, const MyVector & rhs) {
for (size_t i = 0; i < lhs.storage_.size(); ++i) {
lhs.storage_[i] = std::max(lhs.storage_[i], rhs.storage_[i]);
}
return lhs;
}
template<typename T>
T do_max(const T & x, const T & y) {
// if this is std::max then it won't compile for MyVector
return max(x, y);
}
int main(int argc, char * argv[]) {
MyVector v0 { 0.1, 0.2, 0.3, 0.4 };
MyVector v1 { 0.4, 0.3, 0.2, 0.1 };
MyVector v2 = do_max(v0, v1);
// Comment out the following line to successfully compile.
// However I'd like this to work for scalar types too:
float f0 = do_max(0.1f, 0.2f);
return 0;
}
我觉得我需要一种方法来实现 max
函数解析为 std::max
对于标量类型,以及我专门的 max
MyVector 类型的友元函数。
如何定义以这种方式工作的 max 函数?忘记std::max
会更好吗?并使用我自己专用于 MyVector
的 max 函数还提供标量类型的实现,如 float
?
这样做的背景是我正在实现一个数据路径,我希望它能与 MyVector 和标量类型一起使用(作为编译时的参数化类型)。我已经有了算术运算,但是 max
的解决方案将与其他函数一起使用,如 min
, exp
, pow
最佳答案
经典方案:
template<typename T>
T do_max(const T & x, const T & y) {
using std::max;
return max(x, y);
}
参数相关查找找到您的 max
, 对于 float
这不会发生,你会得到 std::max<float>
.
关于C++ - 如何在不重载比较运算符的情况下为 std::max 专门化自定义类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049300/