【发布时间】:2016-05-02 17:34:26
【问题描述】:
我的应用程序使用的是 Boost 版本 1.46.1。 我想将我的应用程序移植到 Boost 版本 1.58.0。 但是我面临一些问题。
我注意到 boost 1.58 对 boost::exception_ptr 的实现与 1.46.1 不同。在 1.46.1 中,boost::exception_ptr 被实现为共享指针:
typedef shared_ptr<exception_detail::clone_base const> exception_ptr;
在 1.58 中,所有实现都封装在一个类中。
class exception_ptr {
typedef boost::shared_ptr<exception_detail::clone_base const> impl;
impl ptr_;
friend void rethrow_exception(exception_ptr const &);
typedef exception_detail::clone_base const *(impl::*unspecified_bool_type)() const;
public:
exception_ptr() {}
explicit exception_ptr(impl const &ptr) : ptr_(ptr) {}
bool operator==(exception_ptr const &other) const { return ptr_ == other.ptr_; }
bool operator!=(exception_ptr const &other) const { return ptr_ != other.ptr_; }
operator unspecified_bool_type() const { return ptr_ ? &impl::get : 0; }
};
由于这些更改,我的代码正在破坏... :(
boost::exception_ptr ExceptionHelper::GetExceptionPtr(MyExceptionPtr_t exception) {
boost::exception_ptr result =
boost::dynamic_pointer_cast<boost::exception_detail::clone_base const>(exception); // This is giving build error
return result;
}
MyExceptionPtr_t ExceptionHelper::TryGetMyExceptionPtr(boost::exception_ptr exception) {
MyExceptionPtr_t result;
boost::shared_ptr<const Exception> constPtr =
boost::dynamic_pointer_cast<const Exception>(exception); // This is giving build error.
if (constPtr) {
result = boost::const_pointer_cast<Exception>(constPtr);
}
return result;
}
std::string ExceptionHelper::GetThrowFilename(const boost::exception_ptr exception) {
std::string result;
if (exception) {
if (boost::get_error_info<boost::throw_file>(*exception)) // This is giving build error.
{
result = *boost::get_error_info<boost::throw_file>(*exception);
}
}
return result;
}
您能否建议我,如何解决上述错误??
谢谢
【问题讨论】:
-
这段代码中
MyExceptionPtr_t的作用是什么,为什么不能用boost::exception_ptr代替? -
这是我自定义的异常类,我所有的异常都应该从这个类派生。 typedef boost::shared_ptr
MyExceptionPtr_t;和类 MyException : public virtual std::exception, public virtual boost::exception { ---- };
标签: c++ api boost compatibility boost-exception