为什么我的衍生自CRTP函数工作
Why does my derived from CRTP function work?
我正在尝试编写一个函数,检查类是否从基类派生。基类遵循奇怪的递归模板模式,也就是说,它是这样定义的:
template<class Derived>
class Base{
派生类是这样派生的:
class Derived: Base<Derived>{
检查一个类是否以这种方式派生,我写了这个检查器:
template<class T>
constexpr bool basedOn(){
return std::is_base_of<Base<T>,T>::value;
}
它成功了!我很惊讶。除非直接从Base派生,否则似乎没有任何东西返回true。为什么它会起作用?使用CRTP派生的类的完整类型是什么?
Derived
的完整类型就是Derived
。但是它确实继承了类型Base<Derived>
,就像您定义它一样。
相关文章:
- 将具有固定签名的自定义函数名称注入 CRTP
- 检测使用 CRTP 的类中的成员函数
- 如何断言 CRTP 的函数为最终函数?
- 使用CRTP将基类的子类传递给构造函数
- 如何在 CRTP 中实现析构函数?
- 仅函数的多重继承 - 没有虚拟和 CRTP
- C++使用 CRTP 模板模板基类中的构造函数
- 从具有泛型返回类型的 crtp 基类调用派生类中的函数
- 解决 CRTP 函数重载歧义问题
- CRTP 在父级析构函数中调用子函数
- 为什么在此 CRTP 基函数调用中添加引用会消除错误?
- 我可以将CRTP与虚拟函数或函数一起使用,以供访问者算法更改的访问者算法
- CRTP 和复制/移动赋值/构造函数继承
- 如何在 CRTP 中调用派生的模板化函数
- 在执行 SFINAE 时访问模板派生类 (CRTP) 的静态函数时类型不完整
- 为什么CRTP(奇怪的递归模板模式)试图选择另一个私有基类的另一个同名函数
- 另一个命名空间和 CRTP 中的模板友元函数
- 使用CRTP时,如何调用派生类的构造函数
- 使用派生的模板参数类型作为函数 (CRTP) 的返回类型
- 为什么我的衍生自CRTP函数工作