简体   繁体   中英

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, ie, 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);
};

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. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM