我可以复制构造一个带有错误信息的提升::异常吗?
Can I copy-construct a boost::exception with the error info?
考虑以下使用 boost 异常类的代码:
class exception : virtual public boost::exception {
// ...
};
template<typename Exc>
class exception_impl : virtual public std::exception
, public Exc {
public:
exception_impl(const Exc& exc) : Exc(exc) {}
virtual const char* what() const throw() {return "blah";}
};
(实际上,此代码更复杂。例如,仅当后者还不是Exc
的直接或间接基类时,exception_impl
才从std::exception
派生。但这只会分散我对问题的注意力,所以我跳过了它。
鉴于此,我现在可以派生自己的异常类:
class some_exception : public exception {
// ...
};
并使用它们:
struct tag_test_int;
typedef boost::error_info<tag_test_int,int> test_int_info;
void f()
{
boost::throw_exception( exception_impl<some_exception>() << test_int_info(42) );
}
但是,事实证明,生成的异常没有test_int_info
对象。因此,我更改了exception_impl
构造函数以提供一些诊断信息:
exception_impl(const Exc& exc)
: Exc(exc) {
std::cerr << "========================================================================nexc:n";
std::cerr << boost::diagnostic_information(exc);
std::cerr << "========================================================================n*this:n";
std::cerr << boost::diagnostic_information(*this);
std::cerr << "========================================================================n";
}
这确实表明,当我将Exc
对象复制到exception_impl
基类对象中时,信息会丢失:
除名: 投掷位置未知(考虑使用 BOOST_THROW_EXCEPTION) 动态异常类型:some_exception [tag_test_int*] = 42 ======================================================================== *这: 投掷位置未知(考虑使用 BOOST_THROW_EXCEPTION) 动态异常类型:exception_impl std::exception::what: "blah">
IIRC,异常对象必须根据标准可复制,并且忽略可能的优化,复制抛出表达式的结果。因此,boost的异常必须是可复制的,并且它们当然不会在此过程中丢失信息。我一定在这里错过了一些相当明显的东西。
我做错了什么?
对我来说效果很好:
(我不得不向exception_impl添加一个默认构造函数)
#include <iostream>
#include <exception>
#include <boost/exception/all.hpp>
using std::cout;
class myException : public virtual boost::exception {
};
template <class T>
class exception_impl : public virtual std::exception, public T {
public:
exception_impl() {}
exception_impl(const T& ex) : T(ex) {}
virtual const char* what() const throw() {return "blah";}
};
class some_exception : public myException {
};
struct tag_test_int;
typedef boost::error_info<tag_test_int,int> test_int_info;
void f()
{
boost::throw_exception( exception_impl<some_exception>() << test_int_info(42) );
}
int main() {
try {
f();
} catch (boost::exception& e) {
cout << boost::diagnostic_information(e);
}
return 0;
}
输出:
Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: N5boost16exception_detail10clone_implI14exception_implI14some_exceptionEEE
std::exception::what: blah
[P12tag_test_int] = 42
编译使用:
- g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
- 提升 1.55
我认为问题是您在构造函数中输出诊断信息,并且tag_test_int没有设置喷射。
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的代码中有错误,未声明的标识符
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID
- 一个带有参数的程序,为什么此程序有错误的答案
- 构建 vcxproj 文件给我一个无信息的错误"The system cannot find the path specified"
- 仅使用boost.datetime在Windows上有错误
- 使用向量循环时,是否有错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 继承默认构造函数在 gcc 中失败并在 clang 中工作,哪个有错误?
- 为什么有错误未定义_getws_s
- 在快速排序程序中有错误