概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。
标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。
类型支持(基本类型、RTTI、类型特性)
指定一个类型派生自另一类型
std::derived_from
定义于头文件 | ||
template< class Derived, class Base > concept derived_from = | (C++20 起) |
概念 derived_from<Derived, Base> 若且唯若忽略 cv 限定符后 Base
是类类型,且它为 Derived
或是 Derived
的公开无歧义基类才得到满足。
注意当 Base
是 Derived
的私有或受保护基类时,此行为异于 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
标签:std,false,derived,value,base,C++,20,convertible
From: https://blog.csdn.net/qq_40788199/article/details/140423903