繁体   English   中英

C++ C4834 错误从 VS2017 移动到 VS2019

[英]C++ C4834 error moving from VS2017 to VS2019

我收到了这个警告,直到现在我没有收到任何警告:

错误 C2220:以下警告被视为错误
警告 C4834:丢弃具有“nodiscard”属性的 function 的返回值

template <typename T>
static void quick_hull(std::vector<T> &points)
{
    points = hull(points);
    // Remove successive duplicates
    std::unique(begin(points), end(points), [](const T &a, const T &b) { return (a.x == b.x) && (a.y == b.y); });
} 
 

你能解释一下为什么会这样吗?

std::unique不删除元素。 它仅以将所有唯一值移到前面的方式对它们进行重新排序。 然后它返回一个迭代器,指向最后一个唯一值之后的元素。

您需要此迭代器以使用特定于容器的方法实际删除重复元素。 没有它,您将不知道重复项从哪里开始,MSVC 会警告您,您正在丢弃这个重要的返回值。

你的代码应该是

auto new_end = std::unique(begin(points), end(points), ...); // move duplicates to end
points.erase(new_end, end(points)); // actually removes duplicates from vector

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM