C++标准模板(STL)- 概念库 (C++20) - 指定一个类型派生自另一类型 - (std::derived_from)

概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。

标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。

类型支持(基本类型、RTTI、类型特性)

指定一个类型派生自另一类型

std::derived_from

定义于头文件 <concepts>

template< class Derived, class Base >

concept derived_from =
  std::is_base_of_v<Base, Derived> &&

  std::is_convertible_v<const volatile Derived*, const volatile Base*>;
(C++20 起)

概念 derived_from<Derived, Base> 若且唯若忽略 cv 限定符后 Base 是类类型,且它为 Derived 或是 Derived 的公开无歧义基类才得到满足。

注意当 BaseDerived 的私有或受保护基类时,此行为异于 std::is_base_of

参阅

is_base_of

(C++11)

检查一个类型是否派生自另一个类型
(类模板)

is_convertibleis_nothrow_convertible

(C++11)(C++20)

检查是否能转换一个类型为另一类型
(类模板)

调用示例 (c++11) 

#include <iostream>
#include <type_traits>

class E
{
public:
    template<class T> E(T&&) { }
};

class A {};

class B : public A {};

class C {};

class D
{
public:
    operator C()
    {
        return c;
    }  C c;
};


//自定义的实现
namespace std
{
template< class Derived, class Base >
constexpr bool derived_from =
    ((std::is_base_of<Base, Derived>::value) &&
     (std::is_convertible<const volatile Derived*, const volatile Base*>::value));
}

int main()
{
    std::cout << std::boolalpha;

    std::cout << "std::is_base_of<B, A>::value:       "
              << std::is_base_of<B, A>::value << std::endl;
    std::cout << "std::is_base_of<A, B>::value:       "
              << std::is_base_of<A, B>::value << std::endl;
    std::cout << "std::is_base_of<B*, A*>::value:       "
              << std::is_base_of<B*, A*>::value << std::endl;
    std::cout << "std::is_base_of<A*, B*>::value:       "
              << std::is_base_of<A*, B*>::value << std::endl;
    std::cout << "std::is_base_of<B*, C*>::value:       "
              << std::is_base_of<B*, C*>::value << std::endl;
    std::cout << "std::is_base_of<D, C>::value:         "
              << std::is_base_of<D, C>::value << std::endl;
    std::cout << "std::is_base_of<int, double>::value:  "
              << std::is_base_of<int, double>::value << std::endl;
    std::cout << "std::is_base_of<int, char>::value:    "
              << std::is_base_of<int, char>::value << std::endl;
    std::cout << "std::is_base_of<std::string, char>::value:    "
              << std::is_base_of<std::string, char>::value << std::endl;

    // 完美转发构造函数使类能从任何类型转换
    std::cout << "std::is_base_of<A, E>::value:         "
              << std::is_base_of<A, E>::value << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::endl;


    std::cout << "std::is_convertible<B, A>::value:       "
              << std::is_convertible<B, A>::value << std::endl;
    std::cout << "std::is_convertible<A, B>::value:       "
              << std::is_convertible<A, B>::value << std::endl;
    std::cout << "std::is_convertible<B*, A*>::value:       "
              << std::is_convertible<B*, A*>::value << std::endl;
    std::cout << "std::is_convertible<A*, B*>::value:       "
              << std::is_convertible<A*, B*>::value << std::endl;
    std::cout << "std::is_convertible<B*, C*>::value:       "
              << std::is_convertible<B*, C*>::value << std::endl;
    std::cout << "std::is_convertible<D, C>::value:         "
              << std::is_convertible<D, C>::value << std::endl;
    std::cout << "std::is_convertible<int, double>::value:  "
              << std::is_convertible<int, double>::value << std::endl;
    std::cout << "std::is_convertible<int, char>::value:    "
              << std::is_convertible<int, char>::value << std::endl;
    std::cout << "std::is_convertible<std::string, char>::value:    "
              << std::is_convertible<std::string, char>::value << std::endl;

    // 完美转发构造函数使类能从任何类型转换
    std::cout << "std::is_convertible<A, E>::value:         "
              << std::is_convertible<A, E>::value << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::endl;

    std::cout << "std::derived_from<B, A>:       "
              << std::derived_from<B, A> << std::endl;
    std::cout << "std::derived_from<A, B>:       "
              << std::derived_from<A, B> << std::endl;
    std::cout << "std::derived_from<B*, A*>:       "
              << std::derived_from<B*, A*> << std::endl;
    std::cout << "std::derived_from<A*, B*>:       "
              << std::derived_from<A*, B*> << std::endl;
    std::cout << "std::derived_from<B*, C*>:       "
              << std::derived_from<B*, C*> << std::endl;
    std::cout << "std::derived_from<D, C>:         "
              << std::derived_from<D, C> << std::endl;
    std::cout << "std::derived_from<int, double>:  "
              << std::derived_from<int, double> << std::endl;
    std::cout << "std::derived_from<int, char>:    "
              << std::derived_from<int, char> << std::endl;
    std::cout << "std::derived_from<std::string, char>:    "
              << std::derived_from<std::string, char> << std::endl;

    // 完美转发构造函数使类能从任何类型转换
    std::cout << "std::derived_from<A, E>:         "
              << std::derived_from<A, E> << std::endl;

    return 0;
}

输出

std::is_base_of<B, A>::value:       false
std::is_base_of<A, B>::value:       true
std::is_base_of<B*, A*>::value:       false
std::is_base_of<A*, B*>::value:       false
std::is_base_of<B*, C*>::value:       false
std::is_base_of<D, C>::value:         false
std::is_base_of<int, double>::value:  false
std::is_base_of<int, char>::value:    false
std::is_base_of<std::string, char>::value:    false
std::is_base_of<A, E>::value:         false
-----------------------------------------------

std::is_convertible<B, A>::value:       true
std::is_convertible<A, B>::value:       false
std::is_convertible<B*, A*>::value:       true
std::is_convertible<A*, B*>::value:       false
std::is_convertible<B*, C*>::value:       false
std::is_convertible<D, C>::value:         true
std::is_convertible<int, double>::value:  true
std::is_convertible<int, char>::value:    true
std::is_convertible<std::string, char>::value:    false
std::is_convertible<A, E>::value:         true
-----------------------------------------------

std::derived_from<B, A>:       true
std::derived_from<A, B>:       false
std::derived_from<B*, A*>:       false
std::derived_from<A*, B*>:       false
std::derived_from<B*, C*>:       false
std::derived_from<D, C>:         false
std::derived_from<int, double>:  false
std::derived_from<int, char>:    false
std::derived_from<std::string, char>:    false
std::derived_from<A, E>:         false

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 00:42:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 00:42:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 00:42:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 00:42:02       55 阅读

热门阅读

  1. 数组指针跟指针数组的区别

    2024-07-22 00:42:02       17 阅读
  2. OpenWRT/iStoreOS 安装 qemu-guest-agent

    2024-07-22 00:42:02       16 阅读
  3. 计算机学院——秋招的总结

    2024-07-22 00:42:02       17 阅读
  4. go中map

    go中map

    2024-07-22 00:42:02      17 阅读
  5. 计算并输出杨辉三角形的前10行

    2024-07-22 00:42:02       21 阅读
  6. 线程局部变量共享 -- 使用ThreadLocal解决该需求

    2024-07-22 00:42:02       15 阅读
  7. 内联汇编清楚变量指定位

    2024-07-22 00:42:02       18 阅读
  8. 信竞2024年csp-j模拟赛第二场赛后总结

    2024-07-22 00:42:02       22 阅读