代码之家  ›  专栏  ›  技术社区  ›  jww avp

错误:“uncaught_exceptions”不可用:在macOS 10.12中引入

  •  1
  • jww avp  · 技术社区  · 6 年前

    我正在测试一个最近在C++ 17下进行测试时出现的问题。以下是源文件:

    $ cat test.cxx
    #if __EXCEPTIONS && __has_feature(cxx_exceptions)
    # include <exception>
    # define CXX17_EXCEPTIONS 1
    #endif
    
    void Foo()
    {
    #if defined(CXX17_EXCEPTIONS)
        if (std::uncaught_exceptions() == 0)
    #endif
        {
            int x = 0;
        }
    }
    

    并在OS X 10.8或10.9上使用Macports编译器编译:

    $ /opt/local/bin/clang++-mp-5.0 -std=gnu++17 test.cxx -c
    test.cxx:9:14: error: 'uncaught_exceptions' is unavailable: introduced in macOS 10.12
        if (std::uncaught_exceptions() == 0)
                 ^
    /opt/local/libexec/llvm-5.0/include/c++/v1/exception:130:63: note:
          'uncaught_exceptions' has been explicitly marked unavailable here
    _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_e...
                                                                  ^
    1 error generated.
    

    /opt/local/bin/clang++-mp-5.0 -std=gnu++17 所以我不能再测试了。

    Clang 3.6 Release Notes ,我相信我使用了正确的测试 std::uncaught_exceptions() :

    为了可靠地测试是否启用C++异常,使用“γ例外”和“安培”; 在对象Obj+C++文件中所有的CLAN版本都不能工作。

    我可能无法包含特定于苹果的头文件,因此无法测试OSX10.12。其他人也经历过这个问题,但我没有找到合适的解决办法。例如,这个 bug report


    $ /opt/local/bin/clang++-mp-6.0 --version
    clang version 6.0.1 (tags/RELEASE_601/final)
    Target: x86_64-apple-darwin13.4.0
    Thread model: posix
    InstalledDir: /opt/local/libexec/llvm-6.0/bin
    
    $ /opt/local/bin/clang++-mp-5.0 --version
    clang version 5.0.2 (tags/RELEASE_502/final)
    Target: x86_64-apple-darwin13.4.0
    Thread model: posix
    InstalledDir: /opt/local/libexec/llvm-5.0/bin
    
    3 回复  |  直到 6 年前
        1
  •  3
  •   Louis Dionne    6 年前

    这里的问题是运行库( libc++.dylib )Mac OS 10.12之前的版本不包含 std::uncaught_exceptions()

        2
  •  1
  •   MerryMage    6 年前

    __EXCEPTIONS && __has_feature(cxx_exceptions) 测试是否存在异常。异常是一个语言特性,自从1990以来(C++中没有引入17),它已经在C++中出现了。,所以除非显式禁用异常,否则它们应该始终可用。

    __cpp_lib_uncaught_exceptions

        3
  •  0
  •   jww avp    6 年前

    我不确定这是否是处理这种情况的正确方法,但它可能会帮助其他人。

    $ cat test.cxx
    #if __EXCEPTIONS && __has_feature(cxx_exceptions)
    # if __cpp_lib_uncaught_exceptions
    #  include <exception>
    #  define CXX17_EXCEPTIONS 1
    # endif
    #endif
    
    void Foo()
    {
    #if defined(CXX17_EXCEPTIONS)
        if (std::uncaught_exceptions() == 0)
    #endif
        {
            int x = 0;
        }
    }
    

    __EXCEPTIONS && __has_feature(cxx_exceptions) 特定于CLAN,并检测是否存在C++ 17异常(包括ObjvEc++)。 __cpp_lib_uncaught_exceptions 是C++语言的特性,并检测C++ 17异常是否可用。

    我不清楚怎么做 __has_feature(cxx_exceptions) 当C++ 17异常不可用时返回true。显然(对我来说)这个功能不可用。

    另请参见 LLVM Issue 39631, __has_feature(cxx_exceptions) returns true but fails to compile std::uncaught_exceptions()