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;
}

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

最近更新

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

    2023-12-08 02:58:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 02:58:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 02:58:04       87 阅读
  4. Python语言-面向对象

    2023-12-08 02:58:04       96 阅读

热门阅读

  1. 浅话人工智能和深度学习

    2023-12-08 02:58:04       50 阅读
  2. Google排名高的是什么样的页面?

    2023-12-08 02:58:04       62 阅读
  3. 第二十四章 控制到 XML 模式的映射

    2023-12-08 02:58:04       49 阅读
  4. docker 如何在容器内重启 php

    2023-12-08 02:58:04       58 阅读
  5. Android 横竖屏切换 窗口全屏

    2023-12-08 02:58:04       63 阅读
  6. [LeetCode] 12. 整数转罗马数字

    2023-12-08 02:58:04       46 阅读
  7. C#中的Lambda表达式

    2023-12-08 02:58:04       59 阅读
  8. Kubernetes实战(七)-反向提取镜像Dockerfile

    2023-12-08 02:58:04       39 阅读