当前位置:网站首页>Implement is by yourself_ convertible
Implement is by yourself_ convertible
2022-07-25 05:11:00 【Hair like snow ty】
c++ Provided in the standard library std::is_convertible, Used to determine whether it can be implicitly converted from one type to another , Here's an example :
class Ax
{
};
class Bx:public Ax
{
public:
};
class Cx
{
};
int main()
{
cout << std::is_convertible<Bx, Ax>::value << endl;
cout << std::is_convertible<Ax, Bx>::value << endl;
cout << std::is_convertible<float, int>::value << endl;
cout << std::is_convertible<int, float>::value << endl;
system("pause");
return 0;
}
result :
After understanding its function , We try to achieve it by ourselves .
template<typename FROM,typename TO>
struct ISCONVERTIBLE
{
private:
static void testfunc(TO);
template<typename = decltype(testfunc(std::declval<FROM>()))>
static std::true_type test(void *);
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(nullptr))::value;
};
The above is the implementation code , Use overloaded functions to realize , Return respectively std::true_type,std::false_type; If FROM Can be converted into TO type , Then it will match std::true_type Of test() Member functions , Otherwise, it will match std::false_type Of test Member functions . Be careful : If FROM The type can be successfully converted to TO type , Then through the decltype infer testfunc() The return type of is valid ,test The function will return std::true_type, Otherwise, the compiler will match and return std::false_type Member function of . Let's do a test :
边栏推荐
- [small program practice] first day
- four hundred and forty-four thousand one hundred and forty-one
- [sht30 temperature and humidity display based on STM32F103]
- Implementation of recommendation system collaborative filtering in spark
- Logu p3398 hamsters find sugar solution
- Which side of Nacos has the SQL script of this column?
- Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
- The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup
- Compile ue5.0
- rhce第一天
猜你喜欢
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

深圳随到随考,科目四随到随考,科三理论第二理论随到随考说明
![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

Introduction to kubernetes

OA and fansoft Bi cross system users, departments and posts synchronous summary
[small program practice] first day

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

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

Unity LOD

Style transfer -- CCPL: contrast coherence preserving loss for versatile style transfer
随机推荐
自己实现is_convertible
Three must know and know problems of redis
Information System Project Manager --- Chapter IX examination questions of project human resource management over the years
Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
China trifluoroethanol industry research and investment forecast report (2022 Edition)
If you don't know these 20 classic redis interview questions, don't go to the interview!
How to judge whether it is attacked by DDoS
Execution process of NPM run XX
rhcsa暑假第三天
自己实现is_base_of
Most of the time, it's probability
ping命令
Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
Leetcode55. Jumping game
STM32 development note 120: solve the problem that%f in printf cannot be output
deep报错
Go language function
Sword finger offer II 014. anagrams in strings
1310_一个printf的实现分析
[wechat applet] label (86/100)