当前位置:网站首页>自己实现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;
}
边栏推荐
- Introduction to base ring tree
- Analysis of lottery winning numbers in history
- JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
- 教你如何定位不合理的SQL?并优化之
- Wechat official account all article download links to get
- Execution process of NPM run XX
- H5 new feature domcontentloaded event - custom attribute -async attribute -history interface method - local storage -postmessage
- Novel capture practice
- [no title] 1
- [small program practice] first day
猜你喜欢

2022-7-13 summary

85 distributed project construction

Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems

rhce第一天

JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening

Teach you three ways to optimize the performance from 20s to 500ms

Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~

基环树入门

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies

How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories
随机推荐
Solve the problem that uni app applet obtains routes and route parameters
How to ensure data consistency between MySQL and redis?
Getting started with scratch
搭建私有CA服务器
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
Implementation principle of epoll
Nexttick principle analysis
OA and fansoft Bi cross system users, departments and posts synchronous summary
Execution process of NPM run XX
The second day of rhcsa summer vacation
Teach you three ways to optimize the performance from 20s to 500ms
哪种网站适合物理服务器
Luogu p4281 [ahoi2008] emergency gathering / gathering solution
Panda3D keyboard moving scene
Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
Preliminary understanding of Panda3D particle system
Ping command
Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
Forwarding and sharing function of wechat applet
Logu p3398 hamsters find sugar solution