C++中模板化异常类的多重继承
Multiple inheritance of a templated exception class in C++
为什么:
#include <iostream>
struct base_exc : std::runtime_error
{
base_exc(const std::string& s): std::runtime_error(("base_exc: " + s).c_str()){}
};
struct derived_exc1 : base_exc
{
derived_exc1(const std::string& s): base_exc(("derived_exc1: " + s).c_str()){}
};
struct derived_exc2 : base_exc
{
derived_exc2(const std::string& s): base_exc(("derived_exc2: " + s).c_str()){}
};
template <typename T1, typename T2>
struct binary_exc: T1, T2
{
binary_exc(const std::string& s): T1(s), T2(s){}
};
int main()
{
try{
throw binary_exc<derived_exc2, derived_exc1>("something occured");
}
catch(base_exc const& e)
{
std::cout << e.what() << std::endl;
}
}
输出:
$ g++ -std=c++11 main.cpp && ./main
terminate called after throwing an instance of 'binary_exc<derived_exc2, derived_exc1>'
Aborted (core dumped)
而不是:
$ g++ -std=c++11 main.cpp && ./main
base_exc: something occured
我想实现的:我希望为代码中的某些异常设置两个"正交"分类标准,例如,一个基于代码中的位置(library1_exc
,library2_exc
,...(,另一个基于错误类别(myobject1isoutofbounds_exc
,myobject2isbroken_exc
,.(。
这些反对意见可以使用类似throw binary_exc<library2_exc, myobject1isoutofbounds_exc>(msg)
的东西抛出,我将能够使用以下任一方法捕获它们:
- 第一个派生类
catch(library2_exc const& e)
- 第二个派生类
catch(myobject1isoutofbounds_exc const& e)
- 基类
catch(base_exc const& e)
使用我的前两个代码 - 使用派生类捕获 - 工作正常,但最后一个不能。为什么?这里有反模式吗?
请注意:
- 我阅读了异常多重继承和 https://www.boost.org/doc/libs/1_62_0/libs/exception/doc/using_virtual_inheritance_in_exception_types.html,但我无法弄清楚它们是否/如何与我的问题相关,尤其是模板的使用。
- 我也完全可以使用不使用模板,但我仍然有兴趣了解为什么上面的代码不起作用。
- 对
binary_exc
使用虚拟继承会产生相同的结果。(编辑:当我写票时,我的意思是我尝试了struct binary_exc: virtual T1, virtual T2
( - 在我看来,我的问题推广到 N>2 个继承,但让我们从 2 开始。
您链接的提升文档正是您的问题。 从binary_exc
到base_exc
的转换不明确,因此异常处理程序不匹配。
不使用虚拟继承时,类型binary_exc<derived_exc1, derived_exc2>
的对象有两个base_exc
子对象。 它的布局是这样的:
+----------------------------------------+
| +--------------+ +--------------+ |
| | +----------+ | | +----------+ | |
| | | base_exc | | | | base_exc | | |
| | +----------+ | | +----------+ | |
| | derived_exc1 | | derived_exc2 | |
| +--------------+ +--------------+ |
| binary_exc<derived_exc1, derived_exc2> |
+----------------------------------------+
由于有两个base_exc
子对象,因此binary_exc
对象不能绑定到对base_exc
的引用。 编译器如何知道要将引用绑定到哪个base_exc
对象?
事实上,它不起作用的原因与以下内容无法编译的原因完全相同:
struct base {};
struct derived1 : base {};
struct derived2 : base {};
struct derived3 : derived1, derived2 {};
void foo(const base& b) {}
int main() {
derived3 d3;
foo(d3);
}
解决方案是使用虚拟继承:
struct base_exc : std::runtime_error
{
base_exc(const std::string& s): std::runtime_error(("base_exc: " + s).c_str()){}
};
struct derived_exc1 : virtual base_exc // <--- NOTE: added virtual keyword
{
derived_exc1(const std::string& s): base_exc(("derived_exc1: " + s).c_str()){}
};
struct derived_exc2 : virtual base_exc // <--- NOTE: added virtual keyword
{
derived_exc2(const std::string& s): base_exc(("derived_exc2: " + s).c_str()){}
};
template <typename T1, typename T2>
struct binary_exc: T1, T2
{
binary_exc(const std::string& s): base_exc(s), T1(s), T2(s){} // <-- NOTE: added call to base_exc constructor
};
现场演示
使用虚拟继承,binary_exc
将只有一个base_exc
子对象。 它将像这样布置:
+------------------------------------------------+
| +----------+ +--------------+ +--------------+ |
| | base_exc | | derived_exc1 | | derived_exc2 | |
| +----------+ +--------------+ +--------------+ |
| binary_exc<derived_exc1, derived_exc2> |
+------------------------------------------------+
由于只有一个base_exc
子对象,因此转换不再模棱两可,因此binary_exc
对象可以绑定到对base_exc
的引用。
注意,由于初始化base_exc
需要binary_exc
,因此至少有一个模板类型参数必须是派生自base_exc
的类。 您可以使用一些SFINAE技巧来避免这种情况,但这是另一个问题。
您应该按照本 Boost.Exception 指南中所述使用虚拟继承。特别是在您的情况下,您需要虚拟地从base_exc
派生。
这样,在尝试将具体异常类型强制转换为base_exc
时,可以避免歧义。
- 关于C++中具有多重继承"this"指针的说明
- C++中模板化异常类的多重继承
- 虚拟继承中是否存在多重继承?
- 如何在 c++ 多重继承中调用父非虚函数?
- 多重继承相同的方法名,没有歧义
- 使用enable_if解决多重继承歧义
- 多重继承导致虚假的模糊虚拟函数过载
- 多重继承和访问不明确的元素
- C++ 多重继承:使用基类 A 的实现实现基类 B 的抽象方法
- 多重继承中的派生类的行为类似于聚合
- 为什么我的 Hippomock 期望在使用多重继承时失败
- 带有此指针的模板类多重继承构造函数不起作用?
- 使用多重继承时出现编译错误
- 增强多重继承的序列化
- 多重继承:跳过'virtual'关键字并拒绝菱形层次结构的使用?
- 仅函数的多重继承 - 没有虚拟和 CRTP
- C++多重继承和鸭子类型
- C++两次从文件保存对象读取多重继承
- 异常类的多重继承
- 模板多重继承异常符号错误