#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception_ptr.hpp>
namespace
boost
{
template <class E>
std::string diagnostic_information( E const & e, bool verbose=true );
std::string diagnostic_information( exception_ptr const & p, bool verbose=true );
}
包含有关传入对象的各种诊断信息的字符串值
每个 error_info 对象的字符串表示形式通过对 to_string(x) 的非限定调用推导而来,其中 x 的类型为 error_info<Tag,T>,Boost Exception 为此定义了一个通用重载。它将 x.value() 转换为字符串,并在 error_info<Tag,T> 模板实例化时,按以下顺序尝试绑定函数:
在调用 diagnostic_information 时,使用第一个成功绑定的函数;如果两个重载分辨率都失败,则系统无法将 error_info 对象转换为字符串,并将使用一个未指定的占位符字符串值,而不会发出编译错误。
diagnostic_information 的 exception_ptr 重载等效于:
if( p ) try { rethrow_exception(p); } catch(...) { return current_exception_diagnostic_information(verbose); } else return <unspecified-string-value>;
这是 diagnostic_information 函数的一个可能的输出示例,如 libs/exception/example/example_io.cpp: 中所示:
example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) Dynamic exception type: class boost::exception_detail::clone_impl<struct fopen_error> std::exception::what: example_io error [struct boost::errinfo_api_function_ *] = fopen [struct boost::errinfo_errno_ *] = 2, "No such file or directory" [struct boost::errinfo_file_name_ *] = tmp1.txt [struct boost::errinfo_file_open_mode_ *] = rb