I've posted this class here previously and am thinking about
it again.
#pragma once
#include <exception>
#include <string>
#include <experimental/string_view>
#include <utility> // move
namespace cmw {
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)) {}
explicit failure (char const* w, int tot) {
if(tot>0)whatStr.reserve(tot);
whatStr=w;
}
char const* what () const throw() { return whatStr.c_str(); }
failure& operator<< (::std::string const& s)
{
whatStr.append(s);
return *this;
}
failure& operator<< (char* s)
{
whatStr.append(s);
return *this;
}
failure& operator<< (char const* s);
failure& operator<< (int i);
template <class T>
failure& operator<< (T val)
{
using ::std::to_string;
return *this<<to_string(val);
}
};
}
I've noticed how the constructors and numerous operator<<
implementations are just wrapping std::string functionality.
One idea to try to get rid of the operator<<'s is to add a
variadic constructor to the class. I think I could use
is_arithmetic or is_numeric or something like that to
figure out if I need to convert an argument to a string.
And I run into some trouble with std::string_view
failure& operator<< (::std::experimental::string_view const& s)
{
whatStr.append(s.data(),s.size());
return *this;
}
I can't just call append() and give a string_view. So is
there a way to figure out if an argument is a string_view?
I'm using clang++ 3.8.1. I've been told in newer compilers it
will be possible to pass a string_view to std::string;;append.
But not sure what to do at this time when I don't have support
for that. Anyway, does this sound like a good change to make
to this class? Thanks in advance.
Brian
Ebenezer Enterprises - "Oh, come, all ye faithful,
Joyful and triumphant! Oh, come ye, oh, come ye to Bethlehem."
http://webEbenezer.net