I'm testing for a problem that surfaced recently during testing under C++17. Here is the source file:
$ 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;
}
}
And compiling it using Macports compiler on OS X 10.8 or 10.9:
$ /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
also experiences the issue. It does not appear to be a one-off problem. 4.0 rejects -std=gnu++17
so I can't test back further.
According to Clang 3.6 Release Notes, I believe I am using the correct test for std::uncaught_exceptions()
:
To reliably test if C++ exceptions are enabled, use __EXCEPTIONS && __has_feature(cxx_exceptions), else things won’t work in all versions of Clang in Objective-C++ files.
I probably won't be able to include an Apple specific header file, so I won't be able to test for OS X 10.12. Others have experienced the issue but I have not found a proper solution. For example, this bug report has the same unresolved issue.
Is there a way to work around the issue on Apple platforms that includes standard C++ preprocessor macros and feature testing? If so, what is the method or tests?
$ /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