为什么在开始之前没有降低Deque Iterator的运行时断言:在VC++?

发布于 2025-02-07 17:14:25 字数 389 浏览 1 评论 0原文

void foo (std::deque<Class>& dq)
{
  auto it = dq.begin();
  --it;  // <--- asserts in VC++
  ...
}

上面的代码是一个过于简单的版本,但我的代码中也发生了类似的情况。它在Ubuntu/Linux中正常工作,但通过Visual Studio断言并逐步中止该程序。

表达式:在开始之前无法降低Deque Iterator

有关您的程序如何导致断言失败的信息,请参见VC ++ ...

即使我们没有寻求或取消迭代器的价值,为什么它会对其进行对待作为运行时错误?

void foo (std::deque<Class>& dq)
{
  auto it = dq.begin();
  --it;  // <--- asserts in VC++
  ...
}

Above code is an oversimplified version, but similar is happening in my code. It works fine in Ubuntu/Linux, but asserts and aborts the program with Visual studio.

Expression: cannot decrement deque iterator before begin

For information on how your program can cause assertion failure, see VC++ ...

Even though we are not seeking or dereferencing the value of the iterator, why does it treats it as a runtime error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-02-14 17:14:25

从指定要求的描述 noReferrer“> nekacyBidirectionaliteratorator

- container.begin()进行评估。


请注意,仅当-Container.Begin()被解释时,行为不会不确定。仅仅试图减少开始迭代器的行为是不确定的行为。

来自标准在双向迭代器上,以下前提条件适用于-Rr是双向迭代器时:

前提:存在s,以便r == ++ s

对于dq.begin(),没有这样的s,因此减少的是未定义的行为。

From the description of the named requirement LegacyBidirectionalIterator:

The begin iterator is not decrementable and the behavior is undefined if --container.begin() is evaluated.

Note that behavior isn't undefined only if --container.begin() is dereferenced. Simply attempting to decrement the begin iterator at all is undefined behavior.

From the standard on bidirectional iterators, the following precondition applies to --r when r is a bidirectional iterator:

Preconditions: there exists s such that r == ++s.

There is no such s for dq.begin() and so decrementing it is undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文