Rather than using std::runtime_error
http://www.cplusplus.com/reference/stdexcept/runtime_error/
, I use the following class. Std::runtime_error has just
one constructor that takes a std::string. The following
class also has a constructor that takes a std::string,
and it has a constructor that takes a char const*.
If I comment out the char const* constructor, the sizes of
the text segments of two of my programs increase. The
increases are by 1.7% and 3.6% using Clang 3.4.1. On GCC
4.9.2, the increases are 7.7% and 11.3%. So I think having
that additional constructor is important.
#include <exception>
#include <string>
#include <utility> // move
class failure : public ::std::exception {
::std::string whatStr;
public:
explicit failure (char const* w) : whatStr(w) {}
explicit failure (::std::string w) : whatStr(::std::move(w)) {}
char const* what () const throw()
{ return whatStr.c_str(); }
failure& operator<< (char const* s)
{
whatStr.append(s);
return *this;
}
failure& operator<< (char* s)
{
whatStr.append(s);
return *this;
}
failure& operator<< (::std::string const& s)
{
whatStr.append(s);
return *this;
}
template <class T>
failure& operator<< (T val)
{
using ::std::to_string;
return *this << to_string(val);
}
};
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net