rethrow_if_nested可能的实现
rethrow_if_nested possible implementation
cppreference.com 提供了有关rethrow_if_nested
可能实现的一些详细信息。这些细节是我感兴趣的。虽然大多数表达对我来说似乎是合理的,但can_dynamic_cast
的定义有点令人困惑:
template <class E>
struct can_dynamic_cast
: std::integral_constant<bool,
std::is_polymorphic<E>::value &&
(!std::is_base_of<std::nested_exception, E>::value ||
std::is_convertible<E*, std::nested_exception*>::value)
> { };
我无法想象在编译时(!std::is_base_of<std::nested_exception, E>::value || std::is_convertible<E*, std::nested_exception*>::value)
计算结果变成false
的情况。更具体地说,
- 如果
std::is_base_of<std::nested_exception, E>::value == false
,则!std::is_base_of<std::nested_exception, E>::value == true
- 如果
std::is_base_of<std::nested_exception, E>::value == true
,则 类型E
派生自std::nested_exception
,无论如何我们都可以E*
转换为std::nested_exception*
。
所以,问题是我的讨论有什么问题,为什么can_dynamic_cast
要以这种方式实施?
注意:据我所知,GCC 7实现了上述逻辑。同时,LLVM的实现似乎检查了参数的类型是否是多态的,没有别的:
template <class _Ep>
inline _LIBCPP_INLINE_VISIBILITY
void
rethrow_if_nested(const _Ep& __e, typename enable_if<
is_polymorphic<_Ep>::value
>::type* = 0)
当std::nested_exception
是E
的模糊或无法访问的基础时(例如E
私自继承自std::nested_exception
(,std::is_base_of<std::nested_exception, E>::value
是正确的,但E*
不能转换为std::nested_exception*
。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在C++中,如何在类和函数(可能是模板化的)的头中编写完整的实现
- std::random_device是如何实现的