当前位置:网站首页>自己实现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的成员函数。下面做个测试:
边栏推荐
- Luogu p2391 snow covered problem solution
- Ping command
- Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
- 基环树入门
- Bypass XSS filters in Web Applications
- 956. Highest billboard pressure DP
- STL notes (I): knowledge system
- 【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
- Pyg builds GCN to realize link prediction
- STL notes (II): template and operator overloading
猜你喜欢

Pikachu vulnerability platform exercise
![[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)](/img/01/42de6280191b9c32a7f37d7727bd4f.png)
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
[email protected]研发效能度量指标"/>学习记录[email protected]研发效能度量指标

Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~

When image component in wechat applet is used as background picture
[email protected] R & D effectiveness measurement indicators"/>Learning records [email protected] R & D effectiveness measurement indicators

一篇文章带你读懂Redis的哨兵模式

教你如何定位不合理的SQL?并优化之

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

Gradle test and idea test
随机推荐
How to publish your own NPM package
Ownership in rust -- introduction of rust language Xiaobai 11
If you don't know these 20 classic redis interview questions, don't go to the interview!
Which side of Nacos has the SQL script of this column?
38 lines of PHP code free import database analysis Linux access log
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
Openworm project compilation
The market is right
Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
Redis cluster setup (Windows)
一篇文章带你读懂Redis的哨兵模式
DOM processing in ahooks
Token value replacement of burpsuite blasting
"Niuke | daily question" inverse Polish expression
Purpose of setting novice task period in the integral system
Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
STM32 Development Notes 119: what macros are required to enable FPU?
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
956. Highest billboard pressure DP