Support for building with exceptions disabled - #1146
Conversation
|
Notes:
|
|
I noticed a few places where it still contains throw, which caused build errors for me. Including but not limited to: |
|
I removed the remaining throws and built it successfully without exceptions. https://github.com/t123yh/yaml-cpp/commit/12520bd8817077b1b025d810aa135045c73f4893 |
|
@jbeder what is your opinion about an "exception free" option for yaml-cpp @twestenkarl This is a great PR. I do not have a use case for a exception free environment, but this of course doesn't mean other might not have it. If @jbeder sees fit for this, there are two things that this PR still needs:
|
|
I suppose it's reasonable, but I never felt like it was worth the readability cost - it seems like it's pretty invasive, since yaml-cpp depends on exceptions pretty heavily for the parsing side of things. I'd check to see how relevant this still is in 2026 - compilers surely have improved, embedded systems are more powerful, and the last comment here was 2023. |
1) A new macro YAML_CPP_NORETURN to annotate functions as not returning in dll.h 2) A new function YAML_throw<ExceptionType>(args...) in exception.h this function will throw an exception unless exceptions are disabled in the compiler, detected by checking the pre-defined macro __cpp_exceptions In this case the exception class will be instantiated, and the user-provided function YAML::handle_exception(const char*) will be called on the exception's what() method 3) if exceptions are disabled,and the library's user does not provide YAML::handle_exception, there will be a linker error 4) 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 exceptions sed -i "s/throw \(.*\)<\(.*\)>(/YAML_throw<\1<\2> >(/g" # throw statements for templated exceptions
bea8dce to
83ce3e1
Compare
- renames the YAML_throw function to raise function - changes handler from linker decision to runtime variables - add a second handle_exception_local handler, allowing to handle errors in threads locally - handle exception are always called if available, throwing an exception is the default fallback case - add 'YAML_CPP_USE_EXCEPTIONS' to deactivate throwing exceptions. - add documentation
I also have no clue, I just would like to close all related issues :-). So I made a huge overhaul of the PR:
|
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.
YAML_CPP_NORETURNto annotate functions as not returning indll.hYAML_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 function
YAML::handle_exception(const char*)will be called on the exception'swhat()methodYAML::handle_exception,there will be a linker error
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