当前位置:网站首页>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 .
边栏推荐
- Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
- The second day of rhcsa summer vacation
- If you don't know these 20 classic redis interview questions, don't go to the interview!
- Completed project series Tutorials - smart campus management system
- Information System Project Manager --- Chapter IX examination questions of project human resource management over the years
- four hundred and forty-four thousand one hundred and forty-one
- [wechat applet] label (86/100)
- Blog Description & message board
- Redis cluster setup (Windows)
- Harbor installation
猜你喜欢

Solve the problem that uni app applet obtains routes and route parameters

Li Kou 731. My schedule II

【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)

STL notes (I): knowledge system

Redis的三个必知必会的问题

Gradle test and idea test
![2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f

epoll的实现原理

Implementation principle of epoll

Pikachu vulnerability platform exercise
随机推荐
2022-7-18 summary
Luogu p2391 snow covered problem solution
Sword finger offer II 014. anagrams in strings
Use getifaddrs to obtain the IP address of the local network interface
基环树入门
[globally unique ID] how to handle the ID primary key after dividing the database and table?
AUTOSAR from getting started to mastering 100 lectures (105) - protection mechanism of AUTOSAR timing for functional safety
Bypass XSS filters in Web Applications
STL notes (VIII): container - List
The third day of rhcsa summer vacation
China trifluoroethanol industry research and investment forecast report (2022 Edition)
Sword finger offer II 012. the sum of the left and right subarrays is equal
2022-7-13 summary
When image component in wechat applet is used as background picture
[untitled]
Ffmpeg download and installation
Forwarding and sharing function of wechat applet
Valley p2420 let's XOR solution
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
Pikachu vulnerability platform exercise