"call to deleted constructor of" compiler error for std::runtime_error sub-class
Solution 1
Because of the user-declared destructors in Exception
and EarthException
, implicit generation of the move constructor and move assignment operators for these classes is disabled. And because of the move-only data member std::stringstream
, implicit copy members are deleted.
However all of that is a distraction.
Your what
member is doomed:
virtual const char* what() const throw()
{
return stream_.str().c_str();
}
This creates an rvalue std::string
and then returns a pointer into that temporary. The temporary destructs before the client can ever read the pointer into that temporary.
What you need to do is pass a std::string
down to the std::runtime_error
base class. Then you don't need to hold a stringstream
, or any other data member. The only tricky part is initializing the std::runtime_error
base class with the proper string
:
template <typename TDerived>
class Exception : public std::runtime_error
{
static
std::string
init(const std::string& file, const unsigned line)
{
std::ostringstream os;
os << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
return os.str();
}
public:
Exception(const std::string& file, const unsigned line)
: std::runtime_error(init(file, line))
{
}
private:
<del>std::stringstream stream_;</del>
Now you'll get implicit copy members and things will just work.
Solution 2
Exception(const std::string& file, const unsigned line)
{
stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
}
This constructor does not call its base constructor, so the compiler generates a call to the default constructor, std::runtime_error::runtime_error()
. But std::runtime_error
does not have a default constructor, which is what the error message is telling you. To fix this, read about std::runtime_error
and call one of its constructors.
EDIT: okay, here's the real problem (not that what I address above isn't a problem, too): the template Exception
has a data member of type std::stringstream
; streams cannot be copied, so the compiler can't generate a copy constructor to use for the throw.
Graeme
Updated on August 26, 2020Comments
-
Graeme over 3 years
I have derived an exception class from
std::runtime_error
in order to add support for streaming to exceptions. I am getting a strange compiler error output with clang that I'm not sure how to resolve?clang++ -std=c++11 -stdlib=libc++ -g -Wall -I../ -I/usr/local/include Main.cpp -c Main.cpp:43:19: error: call to deleted constructor of 'EarthException' throw EarthException(__FILE__, __LINE__) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../EarthException.hpp:9:12: note: function has been explicitly marked deleted here struct EarthException : public Exception<EarthException> template <typename TDerived> class Exception : public std::runtime_error { public: Exception() : std::runtime_error("") {} Exception(const std::string& file, const unsigned line) : std::runtime_error("") { stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: "; } virtual ~Exception() {} template <typename T> TDerived& operator<<(const T& t) { stream_ << t; return static_cast<TDerived&>(*this); } virtual const char* what() const throw() { return stream_.str().c_str(); } private: std::stringstream stream_; }; struct EarthException : public Exception<EarthException> { EarthException() {} EarthException(const std::string& file, const unsigned line) : Exception<EarthException>(file, line) {} virtual ~EarthException() {} }; }
UPDATE:
I've now added explicit calls to
std::runtime_error("")
as it was pointed out the default constructor on this was marked as=delete
however the error remains.