当前位置:网站首页>自己实现is_default_constructible
自己实现is_default_constructible
2022-07-24 12:34:00 【发如雪-ty】
首先介绍一下c++标准库中的std::is_default_constructible,这个类模板的主要功能是判断一个类的对象是否能够被默认构造,如下面示例
class Ax
{
};
class Bx
{
public:
Bx(int x)
{
}
};
int main()
{
cout << std::is_default_constructible<Ax>::value << endl;
cout << std::is_default_constructible<Bx>::value << endl;
system("pause");
return 0;
}
结果:
那我们自己如何实现呢?代码如下:
template<typename T>
struct IsConstructibile
{
private:
template<typename = decltype(T())>
static std::true_type test(void*);
template<typename = int>
static std::false_type test(...);
public:
static constexpr bool value = std::is_same<decltype(test(nullptr)),std::true_type>::value;
};
上面实现的方式,采用了成员函数重载的方式实现的,写得比较不错,值得学习。下面解析一下:
(1)有两个同名静态成员函数模板test().第一个test()返回std::true_type,第二个test()返回std::false_type;第一个test(),形参为void *,第二个为…,这是C语言中的省略号形参,可以接受0到任意多个实参。要重点注意decltype(T()),这是实现的关键。
(2)对于这两个test(),调用的时候,编译器会优先选用有具体形参的版本,只有该版本不匹配时才会选择省略号的版本。
(3)对于decltype(test(nullptr),如果传递给IsConstructibile的类型T支持默认构造,那么显然编译器会选择第一个test,那么返回值就为std::true_type,从而使value为true;
如果传递给IsConstructibile的类型T不支持默认构造,那么decltype(T())的写法根本就不成立,根据SFINAE特性,编译器会选择第二个test,返回值为false_type,所以整体值就为false.下面看看测试结果:
边栏推荐
- 雪花算法(PHP)
- 利用huggingface模型翻译英文
- Is there any entrepreneurship project suitable for one person in the early stage of 2W and 3W? Is it OK to be we media?
- for mysql
- 有没有2、3w前期适合一个人干的创业项目呢?做自媒体可以吗?
- Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
- Shell script case ---2
- [function test] test of the project - login and post function
- Say no to blackmail virus, it's time to reshape data protection strategy
- 如何在IM系统中实现抢红包功能?
猜你喜欢

ThinkPHP realizes database backup

基于Kubernetes v1.24.0的集群搭建(二)

With the strong development of cloud native, how should enterprises seize business opportunities
如何用WebGPU流畅渲染百万级2D物体?

Aruba learning notes 04 Web UI -- Introduction to configuration panel

What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency

Basic SQL server operation problems - only when lists are used and identity_ Only when insert is on can the display value be set for the identification column in the table

【功能测试】项目的测试——登录和发布文章功能

Use abp Zero builds a third-party login module (4): wechat applet development

C Advanced - data storage
随机推荐
Buckle practice - 27 score after turning the matrix
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
雪花算法(PHP)
QT notes - realize form adaptation
SQL JOIN 入门使用示例学习左连接、内连接、自连接
Wechat applet - drawing dashboard
QT notes - EventFilter event filter
leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
C进阶——数据的存储
月薪 3万人民币是一种怎样的体验?做自媒体可以达到这种水平吗
SQL multi condition query cannot be implemented
Learn some programming: anti unemployment "vaccine"
Intent jump pass list set
What kind of experience is a monthly salary of 30000 yuan? Can we achieve this level as we media
Is there a free and commercially available website for US media video clips?
计算两个坐标经纬度之间的距离(5种方式)
Snowflake algorithm (PHP)
向勒索病毒说不,是时候重塑数据保护策略
Industry insight | how to better build a data center? It and business should "go together"
如何用WebGPU流畅渲染百万级2D物体?