当前位置:网站首页>自己实现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.下面看看测试结果:
边栏推荐
- 元宇宙更多的功能和作用在于对于传统生活方式和生产方式的深度改造
- Buckle exercise - 32 divided into k equal subsets
- C进阶——数据的存储
- ThinkPHP realizes database backup
- TypeNameExtractor could not be found
- Use typeface to set the text font of textview
- C Advanced - data storage
- Buckle exercise - 35 combination sum II
- [data mining engineer - written examination] sheen company in 2022
- Installation and deployment of ansible
猜你喜欢

突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战

Okaleido tiger NFT即将登录Binance NFT平台

Pushgateway installation and Prometheus configuration

支持刘海屏

How to realize the function of grabbing red envelopes in IM system?
向勒索病毒说不,是时候重塑数据保护策略

QT notes - realize form adaptation

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

QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content

TypeNameExtractor could not be found
随机推荐
With the strong development of cloud native, how should enterprises seize business opportunities
Acwing 92. recursive implementation of exponential enumeration
for mysql
如何最快找出复杂代码运行时的函数调用流程
Open source DNS software powerdns BIND9 mydns
利用huggingface模型翻译英文
Qt Creator怎样更改默认构建目录
Leetcode-81. search rotation sort array II (binary search returns true/false)
Learning materials about red team
Day3: branch structure
Threat hunting collection
Guys, do you need to configure anything to use rocksdb when using flinksql? Or do you need any jar packages
Ah, I am uncle card space
Is there any entrepreneurship project suitable for one person in the early stage of 2W and 3W? Is it OK to be we media?
Buckle practice - 27 score after turning the matrix
leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN
Opencv:08 image pyramid
How to realize the function of grabbing red envelopes in IM system?
Day4: circular structure