Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Would std::runtime_error benefit from having another constructor?

32 views
Skip to first unread message

woodb...@gmail.com

unread,
Apr 5, 2015, 4:24:41 PM4/5/15
to
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

Ian Collins

unread,
Apr 5, 2015, 5:34:13 PM4/5/15
to
woodb...@gmail.com wrote:
> 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.

Relative to what?

If your environment allows exceptions, a few bytes here and there are of
little consequence.

> #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)) {}

Why don't you just use a move constructor?

--
Ian Collins

Öö Tiib

unread,
Apr 5, 2015, 5:41:00 PM4/5/15
to
On Sunday, 5 April 2015 23:24:41 UTC+3, woodb...@gmail.com wrote:
> 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*.

To my knowledge that has changed since C++11 and now
'std::runtime_error' has two constructors, one that
takes 'std::string const&' and other that takes '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.

It depends on how you use the classes. If you pass mostly
string literals then 'char const*' is more fitting parameter.

Ian Collins

unread,
Apr 5, 2015, 6:21:48 PM4/5/15
to
嘱 Tiib wrote:
> On Sunday, 5 April 2015 23:24:41 UTC+3, woodb...@gmail.com wrote:
>> 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*.
>
> To my knowledge that has changed since C++11 and now
> 'std::runtime_error' has two constructors, one that
> takes 'std::string const&' and other that takes 'char const*'.

It has.

--
Ian Collins

woodb...@gmail.com

unread,
Apr 5, 2015, 11:02:54 PM4/5/15
to
On Sunday, April 5, 2015 at 4:41:00 PM UTC-5, Öö Tiib wrote:
> On Sunday, 5 April 2015 23:24:41 UTC+3, woodb...@gmail.com wrote:
> > 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*.
>
> To my knowledge that has changed since C++11 and now
> 'std::runtime_error' has two constructors, one that
> takes 'std::string const&' and other that takes 'char const*'.
>


OK. I thought I was looking at the documentation for
C++ 2011, but it was the 1998 documentation. Sorry about
that. Maybe I'll start using cppreference.com more.

Brian
Ebenezer Enterprises
http://webEbenezer.net
0 new messages