当前位置:网站首页>自己实现is_convertible
自己实现is_convertible
2022-07-25 05:10:00 【发如雪-ty】
c++标准库中提供的std::is_convertible,用来判断是否可以从一个类型隐式转换为另外一个类型,下面看看例子:
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;
}
结果:
在明白了它的功能后,我们试着自己实现。
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;
};
上面是实现代码,使用重载函数的方式实现,分别返回std::true_type,std::false_type;如果FROM能转换成TO类型,那么就会匹配std::true_type 的test()成员函数,否则会匹配std::false_type的test成员函数。注意:如果FROM类型能顺利地转换为TO类型,那么通过decltype推断testfunc()的返回类型就有效,test函数就会返回std::true_type,否则编译器就会匹配返回std::false_type的成员函数。下面做个测试:
边栏推荐
- [c language] custom type (structure ~ enumeration ~ Union)
- Basic knowledge of scratch crawler framework
- OA and fansoft Bi cross system users, departments and posts synchronous summary
- HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
- The market is right
- Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
- 基环树入门
- Implementation of recommendation system collaborative filtering in spark
- Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
- Androd releases jitpack open source project (gradle7.2)
猜你喜欢

Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"

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

Interviewer: explain the core principle of ThreadLocal

2、 Mysql database foundation

Web: compiling big refactoring from 10 to 1

Execution process of NPM run XX

Unity LOD
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
![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

深圳随到随考,科目四随到随考,科三理论第二理论随到随考说明
随机推荐
Pikachu vulnerability platform exercise
panda3d 键盘移动场景
How to ensure data consistency between MySQL and redis?
How to publish your own NPM package
Unity LOD
Now the operator wants to check the answer details of all user questions from Zhejiang University. Please take out the corresponding data
The market is right
Purpose of setting novice task period in the integral system
How to judge whether it is attacked by DDoS
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
STL notes (II): template and operator overloading
Which website is suitable for physical server
nacos中哪边有这个列的sql脚本啊?
Web: compiling big refactoring from 10 to 1
Solve the problem that uni app applet obtains routes and route parameters
The strongest JVM in the whole network is coming!
Novel capture practice
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
Completed project series Tutorials - smart campus management system
China trifluoroethanol industry research and investment forecast report (2022 Edition)