尝试写例外模板构造函数类,因此抛出异常的时候,我可以把可变长度参数,来到某事像这样:模板的构造
namespace miniReader{
namespace Exception{
template<typename deriverEx, typename baseType>
class Exception : public baseType{
std::string errorClass;
mutable std::string exStrMsg;
protected:
static std::stringstream exMsg;
std::string whatMessage;
// Exception(const Exception& other): baseType(""){}
const char* what() const noexcept{
if(exStrMsg.empty()){
exStrMsg = exMsg.str();
}
exMsg.str("");
return exStrMsg.c_str();
}
~Exception() noexcept {
}
public:
Exception(): baseType("") {
}
};
class miniRuntimeException : public Exception<miniRuntimeException, std::runtime_error>{
public:
miniRuntimeException(): Exception() {}
template<typename T, typename...Args>
miniRuntimeException(T first, Args...arg): Exception(){
LOG(first, this);
exMsg << first << " ";
miniRuntimeException(arg...);
}
};
template<typename deriverEx, typename baseType>
std::stringstream Exception<deriverEx, baseType>::exMsg;
}
}
现在我可以写抛出异常这样的:
miniRuntimeException(__FILE__, __LINE__, "Failed opening file", "Runtime Exception")
我已经做了exMsg静态,因为在其他情况下,我可以得到所有的模板参数。所以我的问题是,Exception类中的exMsg是静态的,所以当派生类构造函数被调用时(variadic模板),读取参数是正确的。我找不到一种方法使它不是静态的,因为exMsg只会得到一个模板参数。有没有办法做到这一点?接下来的问题是当我的模板构造函数被调用时,为每个参数创建新对象(是否正确?)以及如何在这种情况下我可以在这些对象之间传递信息,如更新exMsg,因此它将在最终对象中完成?
你的问题一般不是很清楚...... – callyalater