std :: string :: compare(const char *)会抛出异常吗? [英] Can std::string::compare(const char*) throw an exception?

查看:111
本文介绍了std :: string :: compare(const char *)会抛出异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是重载(4)此处

在例外"部分中,重载2、3、5、6(具有pos1和/或pos2参数)被命名为引发 std :: out_of_range .

In the "Exceptions" section, overloads 2,3,5,6 (which have pos1 and/or pos2 parameters) are named as throwing std::out_of_range.

重载(4)没有"pos"参数,但未标记为 noexcept .

Overload (4) does not have "pos" parameters, but it's not marked noexcept.

是否要抛出实现取决于具体实现?

Is it up to the implementation whether it throws or not?

在GCC 7的libstdc ++中,它调用 char_traits< char> :: length char_traits< char> :: compare .这些似乎无法抛出,但没有标记为 noexcept .

In GCC 7's libstdc++, it calls char_traits<char>::length and char_traits<char>::compare. These don't seem to be able to throw, but aren't marked noexcept.

推荐答案

除析构函数,交换函数,移动构造函数和移动赋值运算符外,该标准仅在具有 广泛合同 ,即没有先决条件.此重载要求参数是一个以空字符结尾的字符串,因此标准不会将其标记为 noexcept .

Except for destructors, swap functions, move constructors and move assignment operators, the standard marks a function noexcept only if it has a wide contract, i.e., it has no preconditions. This overload requires the argument to be a null-terminated string, so the standard does not mark it as noexcept.

N3248 :

标记为 noexcept 的功能很难测试

当一个函数标有 noexcept 时,就不可能标记测试失败,尤其是在测试驱动程序中,会引发异常.一个常见的例子是代码可以验证输入函数的先决条件:

Functions marked noexcept are difficult to test

When a function is marked with noexcept it becomes impossible to flag test failures, notably in test drivers, by throwing an exception. A common example would be code that validates preconditions on entry to a function:

T& std::vector<T>::front() noexcept {
 assert(!this->empty());
 return *this->data();
}

在验证测试驾驶员的这种防御性检查时,一种合理的方法是注册一个引发明确定义的,违反先决条件的异常的断言处理程序,测试驱动程序捕获的内容,以确保确实存在适当的 assert 地方.

When validating such defensive checks from a test driver, a reasonable approach is to register an assert-handler that throws a well-defined precondition-violated exception, which the test driver catches to ensure that the appropriate asserts are indeed in place.

...

现在我们可能会争辩说,当 vector 为空,是未定义的行为,因此我们不应期望任何保证.问题在于该库正在指定未定义的行为.对于编译器,此代码是定义完善,并且,如果 assert 引发异常,则程序必须终止以明确的方式阻止测试驱动程序.

Now we might argue that calling the function out-of-contract, when the vector is empty, is undefined behavior so we should not expect any guarantees. The problem is that undefined behavior is being specified by the library; to the compiler, this code is perfectly well defined and, if assert throws an exception, the program must terminate in a well-specified manner, thwarting the test driver.

请注意,这里的问题不是我们在使用断言来查找自己的错误库实现,而是在错误地调用我们库的用户代码中.如果我们取消测试这些防御性断言的能力,我们可能会弄错它们,因此,与传播相比,我们的用户面临着犯下更严重错误的风险意外的异常.

Note that the issue here is not that we are using assertions to find bugs in our own library implementations, but rather in user code that incorrectly calls into our library. If we remove the ability to test these defensive assertions, we may get them wrong, and thus put our users at risk for committing far more serious errors than propagating an unexpected exception.


顺便说一句,由于 [res.on.exception.handling]/5 :

实现可以通过添加非抛出异常规范来增强非虚函数的异常规范.

An implementation may strengthen the exception specification for a non-virtual function by adding a non-throwing exception specification.

... libstdc ++ libc ++ 可以自由标记此重载 noexcept .

... libstdc++ and libc++ are free to mark this overload noexcept.

这篇关于std :: string :: compare(const char *)会抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆