代码之家  ›  专栏  ›  技术社区  ›  Peter Mitrano

浮点乘法能在C++中抛出异常吗?

  •  7
  • Peter Mitrano  · 技术社区  · 8 年前

    前任:

    constexpr double to_meters(double y) noexcept? {
      return y * 10;
    }
    constexpr double x = to_meters(y); // Clang-Tidy warns about possible exception without noexcept
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   Pete Becker    8 年前

    在这里,语言定义并不能给你任何保证,但是由于几乎每一个实现(也就是说,我所知道的没有一个实现)都实现了IEEE-754数学,它不会抛出异常,这不是我担心的事情。更一般地说,抛出异常的浮点数学包必须用C++编写,这是不太可能的。

    浮点 C++

        2
  •  5
  •   idmean    8 年前

    不,浮点乘法通常不会抛出C++异常。

    to_meter

    nothrow which it does 以下内容:

      Finder->addMatcher(
          varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
                  unless(hasAncestor(functionDecl())),
                  anyOf(hasDescendant(cxxConstructExpr(hasDeclaration(
                            cxxConstructorDecl(unless(isNoThrow())).bind("func")))),
                                             //^^^^^^^^^^^^^^^^^^^
                        hasDescendant(cxxNewExpr(hasDeclaration(
                            functionDecl(unless(isNoThrow())).bind("func")))),
                                        //^^^^^^^^^^^^^^^^^^^
                        hasDescendant(callExpr(hasDeclaration(
                            functionDecl(unless(isNoThrow())).bind("func"))))))
              .bind("var"),
          this);
    
        3
  •  0
  •   M.M    8 年前

    noexcept

    std::fetestexcept FE_ flags listed here