当前位置:网站首页>自己实现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的成员函数。下面做个测试:
边栏推荐
- STM32 Development Notes 119: what macros are required to enable FPU?
- Redis cluster setup (Windows)
- Analysis of lottery winning numbers in history
- I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
- Seven suggestions for Server Protection
- Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
- Introduction to FileStream class of C #
- Delivery practice of private PAAS platform based on cloud native
- "Niuke | daily question" inverse Polish expression
- How can I check if the number of RDS links in MySQL suddenly rises?
猜你喜欢

Redis cluster setup (Windows)

小红书携手HMS Core,畅玩高清视界,种草美好生活

rhce第一天

QT download installation tutorial

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

Pychart configuration pyqt5

panda3d 键盘移动场景
![[c language] custom type (structure ~ enumeration ~ Union)](/img/24/f4ccccef5977c6f465284402487fa2.png)
[c language] custom type (structure ~ enumeration ~ Union)

Ping command

Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
随机推荐
Sword finger offer II 014. anagrams in strings
Pyg builds GCN to realize link prediction
STM32 Development Notes 121: Kalman filter I understand
Androd releases jitpack open source project (gradle7.2)
Unity LOD
[c language] custom type (structure ~ enumeration ~ Union)
Ping command
harbor安装
Redis的三个必知必会的问题
Introduction to CpG control network
Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
[small program practice] first day
How to publish your own NPM package
Implementation principle of epoll
H5 new feature domcontentloaded event - custom attribute -async attribute -history interface method - local storage -postmessage
How to ensure data consistency between MySQL and redis?
基于云原生的私有化 PaaS 平台交付实践
Delivery practice of private PAAS platform based on cloud native
Get the parameters of the browser address bar
Deep understanding of pod