【问题标题】:Boost: API changes between 1.46.1 and 1.58.0?Boost:1.46.1 和 1.58.0 之间的 API 变化?
【发布时间】: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


【解决方案1】:

boost::exception_ptr 是默认可构造的、可复制的、可赋值的和等式可比的。这些操作都不允许您提取捕获的异常本身。类本身没有指定其他操作。从 1.46.1 到 1.58.0 并没有改变;唯一的区别是实现已更改,因此更难意外使用不属于指定接口的boost::exception_ptr 的功能(就像您的代码一样)。

唯一可能的其他操作是:

template <class T>
exception_ptr copy_exception( T const & e );    

exception_ptr current_exception();    

void rethrow_exception( exception_ptr const & ep );

rethrow_exception 是您想要的功能。要从 exception_ptr 中提取数据,重新抛出它,然后在 catch 块中处理数据(这与 std::exception_ptr 使用的模型相匹配,因此当您最终移动到支持 C++11 的编译器):

std::string ExceptionHelper::GetThrowFilename(
    const boost::exception_ptr exception)
{
    std::string result;
    if (!exception) return result;
    try {
        boost::rethrow_exception(exception);
    }
    catch (boost::exception const &e) {
        boost::throw_file::value_type const *throw_file_data =
            boost::get_error_info<boost::throw_file>(e)
        if (throw_file_data) {
            result = *throw_file_data;
        }
    }
    return result;
}

我不知道MyExceptionPtr_t 是干什么用的。看起来它可以被 boost::exception_ptr 替换(因此可以使转换函数变得不必要),但是如果没有所有代码,我很难确定。

【讨论】:

  • MyException 是我的自定义 Exception 类,我所有的异常都应该由此派生。 typedef boost::shared_ptr MyExceptionPtr_t;和类 MyException :公共虚拟 std::exception,公共虚拟 boost::exception { public:explicit MyException(const std::string& what); virtual ~MyException() throw() {} virtual const char* what() const throw(); .... /// 返回 boost::diagnostic_information(*this) 的结果 virtual std::string diagnostic_information() const; -------- };
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 2022-01-02
相关资源
最近更新 更多