std::runtime_error 异常的 wstring 包装器?
A wstring wrapper to std::runtime_error exception?
假设我在某处定义了这些函数:
std::string ws_to_s(const wchar_t*);
std::string ws_to_s(const std::wstring&);
std::wstring s_to_ws(const char*);
我写了这个class:
class runtime_error_w : public std::runtime_error {
public:
runtime_error_w() = default;
runtime_error_w(const wchar_t* what_arg)
: runtime_error(ws_to_s(what_arg)) { }
runtime_error_w(const std::wstring& what_arg)
: runtime_error(ws_to_s(what_arg)) { }
virtual const std::wstring what_w() const noexcept {
return ws_to_s(what());
}
};
到目前为止这对我有用,但是这种方法有什么问题吗?
请注意 what_w()
returns 一个 std::string
对象,而不是 const char*
。
根据问题下方的评论,我同意使用这个 class 是一个 坏主意 ,因为当构建 wstring
.
假设我在某处定义了这些函数:
std::string ws_to_s(const wchar_t*);
std::string ws_to_s(const std::wstring&);
std::wstring s_to_ws(const char*);
我写了这个class:
class runtime_error_w : public std::runtime_error {
public:
runtime_error_w() = default;
runtime_error_w(const wchar_t* what_arg)
: runtime_error(ws_to_s(what_arg)) { }
runtime_error_w(const std::wstring& what_arg)
: runtime_error(ws_to_s(what_arg)) { }
virtual const std::wstring what_w() const noexcept {
return ws_to_s(what());
}
};
到目前为止这对我有用,但是这种方法有什么问题吗?
请注意 what_w()
returns 一个 std::string
对象,而不是 const char*
。
根据问题下方的评论,我同意使用这个 class 是一个 坏主意 ,因为当构建 wstring
.