yaml-cpp
yaml-cpp copied to clipboard
Support for building with exceptions disabled
There are currently three issues for supporting compilation without exceptions resolves #196 (closed with no changes) resolves #799 (open, no interaction) resolves #930 (open, no interaction)
these issue mention the following use case: using the library for a project where external constraints force it to build without exceptions (e.g. game development, embedded)
My use case is a C++ application that I compile with exceptions enabled for Linux/Windows but also with emscripten for WebAssembly where I disable exceptions.
- A new macro
YAML_CPP_NORETURNto annotate functions as not returning indll.h - A new function
YAML_throw<ExceptionType>(args...)inexception.hthis function will throw an exception unless exceptions are disabled in the compiler, detected by checking the pre-defined macro__cpp_exceptionsIn this case the exception class will be instantiated, and the user-provided functionYAML::handle_exception(const char*)will be called on the exception'swhat()method - if exceptions are disabled,and the library's user does not provide
YAML::handle_exception, there will be a linker error - all other files have been changed automatedly by running the following sed commands
sed -i "s/throw \([A-Za-z]*\)(\(.*\))/YAML_throw<\1>(\2)/g"throw statements for non-templated exceptionssed -i "s/throw \(.*\)<\(.*\)>(/YAML_throw<\1<\2> >(/g"throw statements for templated exceptions
Notes:
- this commit does not change the CI config to build with exceptions disabled (which it should add), because I am not familiar with the CI config
- I tried running clang-format on the source tree, but it would also change files that are unchanged by this commit
I noticed a few places where it still contains throw, which caused build errors for me. Including but not limited to:
https://github.com/twestenkarl/yaml-cpp/blob/5b1c606801529ed90ee7f3eaf6afc3e51d547562/include/yaml-cpp/depthguard.h#L54
https://github.com/twestenkarl/yaml-cpp/blob/5b1c606801529ed90ee7f3eaf6afc3e51d547562/src/parser.cpp#L83
I removed the remaining throws and built it successfully without exceptions.
https://github.com/t123yh/yaml-cpp/commit/12520bd8817077b1b025d810aa135045c73f4893