当前位置:网站首页>自己实现is_base_of
自己实现is_base_of
2022-07-25 05:10:00 【发如雪-ty】
std::is_base_of是一个c++11标准中用于判断某个类是否是另外一个类父类的类模板。下面看看测试代码:
class Ax
{
};
class Bx:public Ax
{
};
class Cx
{
};
int main()
{
cout << std::is_base_of<Ax,Bx>::value << endl;
cout << std::is_base_of<Bx,Ax>::value << endl;
cout << std::is_base_of<Ax,Ax>::value << endl;
cout << std::is_base_of<int,float>::value << endl;
cout << std::is_base_of<int,int>::value << endl;
system("pause");
return 0;
}
结果:
接下来自己实现,代码如下:
template<typename Base,typename Derived>
struct ISBASEOF
{
private:
template<typename T>
static std::true_type test(T*);
template<typename>
static std::false_type test(void*);
public:
static constexpr bool value = std::integral_constant<bool,
is_class_v<Base>&&
is_class_v<Derived>&&
decltype(test<Base>(static_cast<Derived*>(nullptr)))::value >::value;
};
测试:
从结果可以看出和标准库一样。
代码段test(static_cast<D*>(nullptr))其实调用test函数,如果类D是类B的子类,那么第一个版本的test()函数是合适的,如果类D不是类B的子类,那么第二个版本适合。下面举个例子说明下:
struct PAR
{
};
struct SON :PAR
{
};
struct OTHER
{
};
void f(PAR* p)
{
cout << "f(PAR* p)\n";
}
int main()
{
f(static_cast<PAR*>(nullptr)); //通过
f(static_cast<SON*>(nullptr)); //通过
f(static_cast<OTHER*>(nullptr)); //编译出错
system("pause");
return 0;
}
边栏推荐
- Nexttick principle analysis
- How to get the database creation time?
- STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
- "Niuke | daily question" inverse Polish expression
- 2022-7-13 summary
- How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories
- Li Kou 731. My schedule II
- Idea2021 installation
- Redis集群搭建(Windows)
- Oracle split branches
猜你喜欢

Basic knowledge of scratch crawler framework

Special analysis of data security construction in banking industry

龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航

If you don't know these 20 classic redis interview questions, don't go to the interview!

How to ensure data consistency between MySQL and redis?

epoll的实现原理

Compile ue5.0

Redis集群搭建(Windows)

rhcsa暑假第三天

Information System Project Manager --- Chapter IX examination questions of project human resource management over the years
随机推荐
38 lines of PHP code free import database analysis Linux access log
After watching the latest interview with big manufacturers, these six JVM interview questions were asked
Idea2021 installation
Three must know and know problems of redis
Gradle test and idea test
Render Optimization: repaint and reflow
深圳随到随考,科目四随到随考,科三理论第二理论随到随考说明
Introduction to base ring tree
When we talk about immutable infrastructure, what are we talking about
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
STL notes (II): template and operator overloading
Ping command
Get the parameters of the browser address bar
Why does the official not recommend using UUID as MySQL primary key
Teach you how to locate unreasonable SQL? And optimize it
Purpose of setting novice task period in the integral system
OA and fansoft Bi cross system users, departments and posts synchronous summary
rhce第一天
Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
Set up private CA server