🔨 small refactoring

The solution with a std::runtime_error member is more elegant. It
allows to have std::exception as base class again. However, I still
have no idea why GCC thinks the copy constructor may throw...
This commit is contained in:
Niels Lohmann 2017-03-25 17:25:39 +01:00
parent cc36c65a89
commit c333679a96
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 12 additions and 6 deletions

View file

@ -123,13 +123,13 @@ Extension of std::exception objects with a member @a id for exception ids.
@since version 3.0.0 @since version 3.0.0
*/ */
class exception : public std::runtime_error class exception : public std::exception
{ {
public: public:
/// returns the explanatory string /// returns the explanatory string
virtual const char* what() const noexcept override virtual const char* what() const noexcept override
{ {
return std::runtime_error::what(); return m.what();
} }
/// the id of the exception /// the id of the exception
@ -137,13 +137,16 @@ class exception : public std::runtime_error
protected: protected:
exception(int id_, const char* what_arg) exception(int id_, const char* what_arg)
: std::runtime_error(what_arg), id(id_) : id(id_), m(what_arg)
{} {}
static std::string name(const std::string& ename, int id_) static std::string name(const std::string& ename, int id_)
{ {
return "[json.exception." + ename + "." + std::to_string(id_) + "] "; return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
} }
private:
std::runtime_error m;
}; };
/*! /*!

View file

@ -123,13 +123,13 @@ Extension of std::exception objects with a member @a id for exception ids.
@since version 3.0.0 @since version 3.0.0
*/ */
class exception : public std::runtime_error class exception : public std::exception
{ {
public: public:
/// returns the explanatory string /// returns the explanatory string
virtual const char* what() const noexcept override virtual const char* what() const noexcept override
{ {
return std::runtime_error::what(); return m.what();
} }
/// the id of the exception /// the id of the exception
@ -137,13 +137,16 @@ class exception : public std::runtime_error
protected: protected:
exception(int id_, const char* what_arg) exception(int id_, const char* what_arg)
: std::runtime_error(what_arg), id(id_) : id(id_), m(what_arg)
{} {}
static std::string name(const std::string& ename, int id_) static std::string name(const std::string& ename, int id_)
{ {
return "[json.exception." + ename + "." + std::to_string(id_) + "] "; return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
} }
private:
std::runtime_error m;
}; };
/*! /*!