yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

Support for building with exceptions disabled

Open twestenkarl opened this issue 3 years ago • 3 comments

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.

  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

twestenkarl avatar Oct 21 '22 14:10 twestenkarl

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

twestenkarl avatar Oct 21 '22 15:10 twestenkarl

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

t123yh avatar Mar 23 '23 11:03 t123yh

I removed the remaining throws and built it successfully without exceptions.

https://github.com/t123yh/yaml-cpp/commit/12520bd8817077b1b025d810aa135045c73f4893

t123yh avatar Mar 23 '23 11:03 t123yh