I am creating custom exception class called app_exception
which is derived from runtime_exception
. I want to put multiple arguments in the constructor, but I can't figure out why the code will not compile. I normally use va_start
with ...
, but I'm trying to do this with Parameter Pack.
template <class Base, class... Args>
class app_error final : public std::runtime_error
{
auto init_base(Args... args)
{
return std::to_string(args);
}
auto init_base(Base msg, Args... args)
{
static std::ostringstream stream;
stream << msg;
stream << init_base(std::forward<Args>(args)...);
return stream.str().c_str();
}
public:
using base = std::runtime_error;
app_error(Base msg, Args... args) : base(init_base(msg, args...)) {}
};
I think this is something along the lines, but I'm not really sure. I want to use it like this:
throw app_error{"FAILED: Exception code is ", exceptionInteger, ". Unable to create ", 5, " needed resources."};