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

Is it possible to have template json::parse with noexcept specifier? #1922

Closed
deiuch opened this issue Feb 2, 2020 · 4 comments
Closed

Is it possible to have template json::parse with noexcept specifier? #1922

deiuch opened this issue Feb 2, 2020 · 4 comments

Comments

@deiuch
Copy link

deiuch commented Feb 2, 2020

  • Describe what you want to achieve.
    Have noexcept for json::parse methods if the third argument is false.

  • Describe what you tried.
    I've tried to use this method in my exception-free code, but my IDE (especially ReSharper C++ plugin) warned me about unhandled exceptions from calling this function. Also noexcept specifier makes optimization passes to be more aggressive.

  • Describe which system (OS, compiler) you are using.
    Microsoft Windows 10, MSVC (from Microsoft Visual Studio 2017), CMake (from 3.12), Vcpkg (version 2020.01.17-nohash).

  • Describe which version of the library you are using (release version, develop branch).
    Version available as the last in vcpkg (in library source marked as 3.7.3).

@deiuch
Copy link
Author

deiuch commented Feb 2, 2020

Somehow this was not inserted into issue text, I'll paste it here.

JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json parse(detail::input_adapter&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)

@deiuch
Copy link
Author

deiuch commented Feb 2, 2020

I've found a macro that may affect exception policy of the library.
Is it possible to introduce some macro like JSON_NOEXCEPT that will put the corresponding noexcept declaration to every function like json::parse based on current JSON_NOEXCEPTION macro?

#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception
#define JSON_TRY try
#define JSON_CATCH(exception) catch(exception)
#define JSON_INTERNAL_CATCH(exception) catch(exception)
#else
#include <cstdlib>
#define JSON_THROW(exception) std::abort()
#define JSON_TRY if(true)
#define JSON_CATCH(exception) if(false)
#define JSON_INTERNAL_CATCH(exception) if(false)
#endif

Example:

#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
    /* ... */
    #define JSON_NOEXCEPT noexcept(false)
#else
    /* ... */
    #define JSON_NOEXCEPT noexcept
#endif

/* ... */

    JSON_HEDLEY_WARN_UNUSED_RESULT
    static basic_json parse(detail::input_adapter&& i,
                            const parser_callback_t cb = nullptr,
                            const bool allow_exceptions = true)
    JSON_NOEXCEPT
    {
        /* ... */
    }

/* ... */

// clean up
/* ... */
#undef JSON_NOEXCEPT

@nlohmann
Copy link
Owner

Technically, the parse function is not noexcept, because it allocates memory which can yield std::bad_alloc.

@deiuch
Copy link
Author

deiuch commented Feb 12, 2020

Got it, so no more questions :)
But you still may consider this macro (if it has any sense for some function in public API).

@deiuch deiuch closed this as completed Feb 12, 2020
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