Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exceptions are currently required #70

Closed
seanmiddleditch opened this issue Sep 17, 2014 · 5 comments
Closed

exceptions are currently required #70

seanmiddleditch opened this issue Sep 17, 2014 · 5 comments

Comments

@seanmiddleditch
Copy link

cppformat cannot be compiled with exceptions disabled due to a single try/catch block with an empty catch clause.

A number of platforms and specialized applications have mandatory no-exceptions rules. Embedded apps, games, high-availability, etc. often run on platforms where exceptions are either deemed too dangerous and hard to reason about or run on memory/performance-constrained devices that can't take the code bloat hit of exceptions or even run on platforms with non-conforming C++ implementations that don't support exceptions in the first place.

The ability to disable exceptions in cppformat would be nice. It's easy enough to just comment out the one use of try/catch, but having it Just Work(tm) would be nicer.

Defines to detect exception support include __EXCEPTIONS on GCC/Cland and _HAS_EXCEPTIONS on cl (VC++).

@vitaut
Copy link
Contributor

vitaut commented Sep 18, 2014

The try-catch constructs can be conditionally compiled of course, but what about throw? There are quite a few of them in the library

@seanmiddleditch
Copy link
Author

Hmm, at least with VC++ 18, commenting out those try/catch let it compile. I haven't test with GCC, Clang, Embacadero, etc.

@vitaut
Copy link
Contributor

vitaut commented Oct 3, 2014

GCC gives compile errors for throw statements when compiling with -fno-exceptions. So conditionally compiling the try-catch blocks is not enough.

If you want to use cppformat with the code without exception, you can create a simple wrapper for fmt::format or other formatting function, that catches all exceptions and returns an error code instead. This wrapper and the library can be compiled with exceptions enabled and then linked to the code that is compiled with exceptions disabled.

@seanmiddleditch
Copy link
Author

Another option would be to allow a simple #define to change the throw statements in C++ Format to calls to a wrapper that aborts with the error. Aside from any other arguments I might have, your proposed solution still doesn't solve the issue for C++ implementations that don't support exceptions (or support them with caveats and bugs) or for environments with extremely strict "no exceptions, period" rules.

Something like a CPP_FORMAT_ERROR macro or wrapper can be used in place of raw throw, with something like this supporting it:

#if !defined(CPP_FORMAT_ERROR)
  #if defined(__EXCEPTIONS) || _HAS_EXCEPTIONS
    #define CPP_FORMAT_ERROR(str) throw ::fmt::FormatError(str)
  #else
    #define CPP_FORMAT_ERROR(str) assert(("" str, false))
  #endif
#endif

And a user of the library can then #define their own error handler that calls into their appropriate error handling system, or override assert (which many do already for improved assert handling or platform-specific reasons).

@vitaut
Copy link
Contributor

vitaut commented Oct 6, 2014

Should be done in 8b76e97. Thanks for the suggestion.

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

No branches or pull requests

2 participants