jeudi 22 septembre 2016

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

Aucun commentaire:

Enregistrer un commentaire