Harald
Harald

Reputation: 240

gcc 4.9.1 not standard compliant? (std::runtime_error)

We would like to have a own definition of std::runtime_error:runtime_error(const string& arg). We implement such constructor in terms of the other constructor, i.e., std::runtime_error:runtime_error(const char*), like:

namespace std {
  runtime_error::runtime_error(const string& arg)
    : runtime_error(arg.c_str()) {
    ...
  }
}

With gcc-4.9.1, this is not possible, since the constructor std::runtime_error::runtime_error(const string& arg) does not exist. In gcc/4.9.1/include/c++/4.9.1/stdexcept, we see the following:

...
class runtime_error : public exception 
{
  string _M_msg;
  public:
  /** Takes a character string describing the error.  */
  explicit 
  runtime_error(const string& __arg);
  virtual ~runtime_error() _GLIBCXX_USE_NOEXCEPT;
  /** Returns a C-style character string describing the general cause of
  *  the current error (the same string passed to the ctor).  */
  virtual const char* 
  what() const _GLIBCXX_USE_NOEXCEPT;
};
...

The standard clearly says that there should be an explicit runtime_error(const char*) constructor.

19.2.6 Class runtime_error [runtime.error]

namespace std {
class runtime_error : public exception {
public:
explicit runtime_error(const string& what_arg);
explicit runtime_error(const char* what_arg);
};

Upvotes: 3

Views: 1204

Answers (1)

Blue Demon
Blue Demon

Reputation: 413

Perhaps this does not answer your original question, but if the intention really is to intercept the runtime_error instantiation, you can do like this (asuming gcc is used):

namespace std {
  runtime_error::runtime_error(const string& arg)
  #if (__GNUC__ > 4)
    : runtime_error(arg.c_str())
  #else
    : _M_msg(arg)
  #endif
  {
    // intercept here!
  }
}

Hopefully, the invoked runtime_error(const char*) will not be implemented in the future in terms of runtime_error(const string&), which would break all your hope and desire. =)

Upvotes: 2

Related Questions