当前位置:网站首页>Implement is by yourself_ class
Implement is by yourself_ class
2022-07-25 05:11:00 【Hair like snow ty】
std::is_class yes c++11 In the standard, it is used to judge whether a type is a class type ( But not a union type ) Class template for . The use of it was explained in my previous article . link :is_class Use
Let's take a look at its implementation :
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;
}
result :
As the above code , Implementation method and is_default_constructible similar , They all use member function overloading . For these two test Member functions , The compiler will still prefer the version with specific parameters , Special attention int U::*p, This expression is actually a member variable pointer . It can be understood as the following code :
int A::*p = nullptr;
If U It's a class type , Then grammatically speaking ,int U::*p This kind of writing conforms to the grammatical rules , otherwise , If U Not a class type , This kind of writing does not conform to the grammatical rules , according to SFINAE characteristic , It matches the second one test() edition , And once you match the second test() The version will match successfully .
summary : If passed to ISCLASS Type template parameters for T Is a class type but not a union type , that test() Back to you std::integral_constant<bool,ture> type , Otherwise, the return is std::integral_constant<bool,false> type .
边栏推荐
- Luogu p2391 snow covered problem solution
- Wechat official account all article download links to get
- Logu p3398 hamsters find sugar solution
- Gradle test and idea test
- Implementation of recommendation system collaborative filtering in spark
- RHCE first day
- Preliminary understanding of Panda3D particle system
- Delivery practice of private PAAS platform based on cloud native
- Docker builds MySQL master-slave replication
- Get the parameters of the browser address bar
猜你喜欢

QT download installation tutorial

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies

RHCE first day

Information System Project Manager --- Chapter IX examination questions of project human resource management over the years

panda3d 键盘移动场景

Gradle test and idea test
![2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f

Compile ue5.0

85 distributed project construction

Teach you three ways to optimize the performance from 20s to 500ms
随机推荐
Wechat official account all article download links to get
2022-7-18 summary
harbor安装
STL notes (VI): container vector
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
哪种网站适合物理服务器
2022-7-13 summary
Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
Solve the problem that uni app applet obtains routes and route parameters
Redis cluster setup (Windows)
js 页面增加过渡层
Introduction to base ring tree
Web: compiling big refactoring from 10 to 1
rhce第一天
Render Optimization: repaint and reflow
China trifluoroethanol industry research and investment forecast report (2022 Edition)
Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
Docker builds MySQL master-slave replication
[small program practice] first day
Analysis of lottery winning numbers in history