当前位置:网站首页>自己实现is_class
自己实现is_class
2022-07-25 05:10:00 【发如雪-ty】
std::is_class是c++11标准中用于判断某个类型是否为一个类类型(但不是联合类型)的类模板。对于它的使用在我之前的文章有讲解。链接:is_class使用
下面看看它的实现:
template<typename T>
struct ISCLASS
{
private:
template<typename U>
static std::integral_constant<bool,!std::is_union<U>::value> test(int U::*p);
template<typename>
static std::integral_constant<bool, false> test(...);
public:
static constexpr bool value = decltype(test<T>(nullptr))::value;
};
int main()
{
cout << ISCLASS<Bx>::value << endl;
cout << ISCLASS<Ax>::value << endl;
cout << ISCLASS<int>::value << endl;
cout << ISCLASS<float>::value << endl;
system("pause");
return 0;
}
结果:
如上述代码,实现方法和is_default_constructible类似,都是使用了成员函数重载的方式。对于这两个test成员函数,编译器依旧会优先选择有具体形参的那个版本,特别要注意int U::*p,这个写法其实是一个成员变量指针。可以理解成如下的代码:
int A::*p = nullptr;
如果U是一个类类型,那么从语法上来讲,int U::*p这种写法就符合语法规则,否则,如果U不是一个类类型,这种写法就不符合语法规则,根据SFINAE特性,就会匹配第二个test()版本,而一旦匹配第二个test()版本则一定会匹配成功。
总结:如果传递给ISCLASS的类型模板参数T是一个类类型但不是一个联合类型,那么test()返回的就是std::integral_constant<bool,ture>类型,否则返回的就是std::integral_constant<bool,false>类型.
边栏推荐
- "Niuke | daily question" inverse Polish expression
- Introduction to kubernetes
- STM32 Development Notes 118: using CMSIS DSP Library in stm32cube IDE
- 基环树入门
- HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
- Pyg builds GCN to realize link prediction
- rhcsa暑假第二天
- 服务器防护的七个建议
- 85 distributed project construction
- Redis的三个必知必会的问题
猜你喜欢

Forwarding and sharing function of wechat applet
[email protected] R & D effectiveness measurement indicators"/>Learning records [email protected] R & D effectiveness measurement indicators

Delivery practice of private PAAS platform based on cloud native

Panda3D keyboard moving scene

When image component in wechat applet is used as background picture
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

2、 Mysql database foundation

ping命令

rhcsa暑假第二天

panda3d 键盘移动场景
随机推荐
How to test data in the process of data warehouse migration?
How to publish your own NPM package
Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
Execution process of NPM run XX
"Niuke | daily question" inverse Polish expression
[wechat applet] picker scroll selector (85/100)
Baklib: share some methods about building enterprise knowledge management (km)
Harbor installation
China trifluoroethanol industry research and investment forecast report (2022 Edition)
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
Render Optimization: repaint and reflow
Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems
Preliminary understanding of Panda3D particle system
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
Pyg builds GCN to realize link prediction
1310_一个printf的实现分析
After watching the latest interview with big manufacturers, these six JVM interview questions were asked
Add click event to unity 3D object
STM32 Development Notes 121: Kalman filter I understand