有什么理由使用运行时断言而不是编译时断言?

发布于 2024-09-18 00:29:59 字数 473 浏览 5 评论 0原文

在审查 Visual C++ 代码库时,我发现了以下奇怪的事情。在可以在编译时评估条件的情况下使用运行时断言(即检查条件并在违反条件时抛出异常):

assert( sizeof( SomeType ) == sizeof( SomeOtherType ) );

显然编译器将评估条件 的代码

assert( true );

并替换实际上不执行任何操作或

assert( false );

每次控制通过该行时抛出异常

。 IMO 应该使用编译时断言,原因如下:

  • 它会在编译时更早地暴露条件违规,并且
  • 会发出更干净(因此更快、更小)的机器代码

看起来像编译时断言是唯一正确的事情。这里是否有任何可能的理由更喜欢运行时断言?

While reviewing Visual C++ codebase I found a following strange thing. A run-time assert (which is check the condition and throw an exception if the condition is violated) was used in a case when the condition could be evaluated at compile time:

assert( sizeof( SomeType ) == sizeof( SomeOtherType ) );

clearly the compiler will evaluate the condition and replace the code that will effectively be either

assert( true );

which does nothing or

assert( false );

which throws an exception every time control passes through that line.

IMO a compile-time assert should have be used instead for the following reasons:

  • it would expose the condition violation earlier - at compile time - and
  • it would let cleaner (thus faster and smaller) machine code be emitted

Looks like a compile-time assert is the only right thing. Is there any possible reason to prefer a run-time assert here?

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

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

发布评论

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

评论(2

爱冒险 2024-09-25 00:29:59

这里没有理由更喜欢运行时断言。您应该更喜欢编译时错误而不是运行时错误,因此,考虑到两者之间的选项,永远没有理由选择运行时断言。

但是,如果静态断言不是一种选择(不知道静态断言的概念,不知道如何制作静态断言并且没有可用的静态断言,或者知道如何制作静态断言但没有)的时间),运行时断言是下一个最好的事情。

对于 C++0x,内置的 static_assert 功能应该消除在编译时断言可以工作的情况下使用运行时断言的所有理由。

There's no reason to prefer a run-time assert here. You should prefer compile-time errors over run-time errors so there's never a reason, given the option between the two, to choose a run-time assert.

However, if a static assert isn't an option (doesn't know the concept of a static assert, doesn't know how to make one and doesn't have one available, or knows how to make one but doesn't have the time to), a run-time assert is the next best thing.

With C++0x, the built-in static_assert feature should end all reason to use a run-time assert where a compile-time assert would work.

颜漓半夏 2024-09-25 00:29:59

没有上下文我们无法判断。在模板代码中,某些实例化可能无法访问某些分支。编译时断言是不合适的,因为这会导致整个函数格式错误。 assert() 则不然。

例如

template <typename T> void foo(T t)
{
  if (t < 0) {
    assert(std::numeric_limits<T>::min() < 0);
    T u = t - std::numeric_limits<T>::min();
  }
}

,即使运行时断言永远不会失败,断言也无法转换为静态断言。

We can't tell without context. In template code, some branches might be unreachable for some instantiations. A compile-time assert would be inappropriate, as that renders the entire function illformed. An assert(<type-dependent expression>) does not.

E.g.

template <typename T> void foo(T t)
{
  if (t < 0) {
    assert(std::numeric_limits<T>::min() < 0);
    T u = t - std::numeric_limits<T>::min();
  }
}

The assert cannot be converted to a static assert, even though the run-time assert never fails.

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