Skip to content

Conversation

twestenkarl
Copy link

@twestenkarl twestenkarl commented Oct 21, 2022

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

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
Copy link
Author

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

@t123yh
Copy link

t123yh commented Mar 23, 2023

@t123yh
Copy link

t123yh commented Mar 23, 2023

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants