jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.92k stars 1.78k forks source link

Support for building with exceptions disabled #1146

Open twestenkarl opened 1 year ago

twestenkarl commented 1 year ago

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 commented 1 year ago

Notes:

t123yh commented 1 year ago

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 commented 1 year ago

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

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