当前位置: 首页 > article >正文

C++新经典模板与泛型编程:用成员函数重载实现is_base_of

用成员函数重载实现is_base_of

std::is_base_of是一个C++ 11标准中用于判断某个类是否是另一个类父类的类模板。

#include "killCmake.h"

#include<string>

using namespace std;

class A
{
};

class B : public A
{
public:
	B(int x): x_(x)
	{}
private:
	int x_;
};

//template<typename Base,typename Derived>
//struct is_base_of {...};


int main()
{

	std::cout << std::is_base_of<A, A>::value << std::endl;
	std::cout << std::is_base_of<B, A>::value << std::endl;
	std::cout << std::is_base_of<A, B>::value << std::endl;

	return 0;
}

在这里插入图片描述
C++ 17标准中又引入了变量模板简化std::is_base_of的书写。

#include "killCmake.h"

#include<string>

using namespace std;

class A
{
};

class B : public A
{
public:
	B(int x): x_(x)
	{}
private:
	int x_;
};

//template<typename Base,typename Derived>
//struct is_base_of {...};

template<class Base,class Derived>
inline constexpr bool is_base_of_v_v = std::is_base_of<Base, Derived>::value;

int main()
{

	std::cout << std::is_base_of<A, A>::value << std::endl;
	std::cout << std::is_base_of<B, A>::value << std::endl;
	std::cout << std::is_base_of<A, B>::value << std::endl;

	std::cout << std::endl;
	// 简化版本
	std::cout << is_base_of_v_v<A, A> << std::endl;
	std::cout << is_base_of_v_v<B, A> << std::endl;
	std::cout << is_base_of_v_v<A, B> << std::endl;

	return 0;
}

在这里插入图片描述
std::is_base_of的实现代码,写一个IsBaseOf类模板来实现,代码如下。

#include "killCmake.h"

#include<string>

using namespace std;

class A
{
};

class B : public A
{
public:
	B(int x): x_(x)
	{}
private:
	int x_;
};

//template<typename Base,typename Derived>
//struct is_base_of {...};

template<class Base,class Derived>
inline constexpr bool is_base_of_v_v = std::is_base_of<Base, Derived>::value;

template<typename Base,typename Derived> // <父类,子类>
class IsBaseOf
{
private:
	template<typename T>
	static std::true_type test(T*);

	template<typename>
	static std::false_type test(void*);

	template<typename B,typename D>
	static auto test_middle() -> decltype(test<B>(static_cast<D*>(nullptr)));
	// 调用test()

public:
	static constexpr bool value = IsSameType < std::integral_constant<bool, std::is_class_v<Base>&& std::is_class_v<Derived>&& decltype(test_middle<Base, Derived>())::value
		, std::integral_constant<bool, true>>::value;
};


int main()
{

	std::cout << std::is_base_of<A, A>::value << std::endl;
	std::cout << std::is_base_of<B, A>::value << std::endl;
	std::cout << std::is_base_of<A, B>::value << std::endl;

	std::cout << std::endl;
	// 简化版本
	std::cout << is_base_of_v_v<A, A> << std::endl;
	std::cout << is_base_of_v_v<B, A> << std::endl;
	std::cout << is_base_of_v_v<A, B> << std::endl;

	return 0;
}

未完待续,干他菊花就对了


http://www.kler.cn/news/161535.html

相关文章:

  • java读取微信p12证书信息
  • 鸿蒙原生应用/元服务开发-Stage模型能力接口(一)
  • 【Python3】【力扣题】383. 赎金信
  • python flask Jinja2模板学习
  • elementui el-table用span-method方法对相同的列名或行名进行合并
  • 在Windows 11中,把iPhone照片和视频导出来又快又简单,无需第三方软件
  • 数据结构 图的广度优先搜索和深度优先搜索
  • 画好一张规范的原理图,这些点你可要注意了!
  • Redis RedisHelper
  • 【LeeCode】454. 四数相加 II
  • dbug_hub 错误 使用多个ILA导致
  • STM32 定时器配置步骤
  • Java多线程编程深入解析——Java程序员,你掌握了多线程吗?【文末送书-03】
  • C语言指针——野指针
  • Python中如何判断List中是否包含某个元素
  • cocos creator “TypeError: Cannot set property ‘string‘ of null
  • 【原神游戏开发日志1】缘起
  • 低代码你需要了解一下
  • 【Android】查看keystore的公钥和私钥
  • API接口使用方法(封装好的电商平台)
  • 添加新公司代码的配置步骤-Part4
  • 水平自动扩容和缩容HPA;API资源对象NetworkPolicy;Kubernetes用户安全控制;Kubernetes创建普通用户示例
  • Windows 基于 VMware 虚拟机安装银河麒麟高级服务器操作系统
  • 第3节:Vue3 v-bind指令
  • 华为OD机试 - 攀登者1(Java JS Python C)
  • 030 - STM32学习笔记 - ADC(四) 独立模式多通道DMA采集
  • 电力智慧运维系统
  • 挑选数据可视化工具:图表类型、交互功能与数据安全
  • 2023年12月7日:QT实现登陆界面
  • 【LeetCode热题100】【双指针】三数之和