1

我的应用程序在 Boost 版本 1.46.1 上。我想将我的应用程序移植到 Boost 版本 1.58.0。但是我面临一些问题。

我注意到 boost 1.58boost::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;
}

您能否建议我,如何解决上述错误?

谢谢

4

1 回答 1

4

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(因此可以使转换函数变得不必要),但是如果没有所有代码,我很难确定。

于 2016-01-26T08:18:34.870 回答