vendredi 18 octobre 2019

Why was const char* constructor added to std::runtime_error in c++11?

From https://en.cppreference.com/w/cpp/error/runtime_error :

explicit runtime_error( const std::string& what_arg );
  (1)     
explicit runtime_error( const char* what_arg );
  (2)     (since C++11)

Constructs the exception object with what_arg as explanatory string that can be accessed through what().

Because copying std::runtime_error is not permitted to throw exceptions, this message is typically stored internally as a separately-allocated reference-counted string. This is also why there is no constructor taking std::string&&: it would have to copy the content anyway.

In the const char* version was added. At first this looks like a step in the wrong direction, but knowing how much goes into interface design of , Im left wondering what the reason and motivation behind this decision was ?

Perhaps it's to avoid the extra copy of a std::string in some circumstances. However, this seems somewhat negliable considering what the class is used for.

Aucun commentaire:

Enregistrer un commentaire