如何获取 qint64 数字和标量的最大值?
以下内容无效。我猜是因为 std::max 既不接受也不返回 qint64。
qint64 fromTimeStamp = -1;
qint64 fromTimeStampNonNegative = std::max(fromTimeStamp, 0);
最佳答案
问题不在于qint64
不知道std::max
。它只是 long long
的 typedef 。这不起作用的真正原因是您指定了两种不同的模板类型:
std::max 的(简化)签名是
template <typename T>
T max(T a, T b);
如果你用你的参数调用它,你有 2 种不同的类型 T
。一方面,qint64
,另一方面 int
(根据 0
文字确定)。这不起作用,因为编译器要求两个参数具有相同的类型。
您所要做的就是确保它们是或明确指定模板类型。以下所有示例都将这样做并因此有效:
-
std::max(fromTimeStamp, 0ll);
-
std::max(fromTimeStamp, static_cast<qint64>(0));
-
std::max<qint64>(fromTimeStamp, 0);
关于c++ - 获取 qint64 数字和标量的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53629272/