1

在 boost::exception(或 std::exception)的捕获站点,我想在不知道类型的情况下迭代异常的所有 error_info 元素。我需要提取所有名称-值对。

我想这应该是可能的,因为 boost::diagnostic_information 函数可以做到这一点,但我想避免重复所有这些代码。

这可以做到吗?怎么做?

4

1 回答 1

1

总是有以下信息(如果你使用过BOOST_THROW_EXCEPTION):

char const * const * f=get_error_info<throw_file>(*be);
int const * l=get_error_info<throw_line>(*be);
char const * const * fn=get_error_info<throw_function>(*be);
if( !f && !l && !fn )
    tmp << "Throw location unknown (consider using BOOST_THROW_EXCEPTION)\n";

除此之外,您使用了error_info_container,但该data_成员是私有的¹。

如果您愿意“强制”通过该障碍,那么“复制”的代码就不会那么多:

char const *
diagnostic_information( char const * header ) const
    {
    if( header )
        {
        std::ostringstream tmp;
        tmp << header;
        for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
            {
            error_info_base const & x = *i->second;
            tmp << x.name_value_string();
            }
        tmp.str().swap(diagnostic_info_str_);
        }
    return diagnostic_info_str_.c_str();
    }

那里的一切都是无证的,虽然不是公共 API 的一部分:它存在于命名空间boost::exception_detail和 classboost::exception_detail::exception_info_container_impl中。

简而言之,有龙(这些接口如有更改,恕不另行通知,并且可能取决于令人惊讶的假设)。


¹(一些较旧的编译器除外)。

于 2018-01-10T17:08:06.260 回答