当前位置:网站首页>Implement is by yourself_ default_ constructible
Implement is by yourself_ default_ constructible
2022-07-24 12:39:00 【Hair like snow ty】
Let's first introduce c++ In the standard library std::is_default_constructible, The main function of this class template is to determine whether the object of a class can be constructed by default , As shown in the following example
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;
}
result :
How can we achieve it by ourselves ? The code is as follows :
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;
};
The above implementation , It is implemented by overloading member functions , Well written , Worth learning . Let's analyze :
(1) There are two static member function templates with the same name test(). first test() return std::true_type, the second test() return std::false_type; first test(), The formal parameter is void *, The second is …, This is a C Ellipsis formal parameter in language , Acceptable 0 To any number of arguments . Pay special attention to decltype(T()), This is the key to implementation .
(2) For these two test(), When called , The compiler will prefer the version with specific formal parameters , Only when the version does not match, the ellipsis version will be selected .
(3) about decltype(test(nullptr), If passed to IsConstructibile The type of T Support default construction , Then obviously the compiler will choose the first test, Then the return value is std::true_type, So that value by true;
If passed to IsConstructibile The type of T Default construction is not supported , that decltype(T()) The writing method of is not tenable at all , according to SFINAE characteristic , The compiler will choose the second test, The return value is false_type, So the overall value is false. Let's look at the test results :
边栏推荐
- AcWing 92. 递归实现指数型枚举
- 02 linear structure 2 multiplication and addition of univariate polynomials (linked list solution)
- 自己实现is_default_constructible
- Ape anthropology topic 5
- Buckle practice - 31 effective tic tac toe games
- 猿人学第六题
- Buckle practice - 24 remove repeated letters
- 国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确
- try...finally总结
- Examples of map search
猜你喜欢

Summary of MySQL database combined with actual SQL optimization of the project

Seckill implementation diagram

Behind the rapid growth, Huawei cloud Wulanchabu data center is the green way

QT notes - EventFilter event filter

OpenCV:08图像金字塔

Native Crash的一切

Equal principal increasing repayment / equal principal decreasing mortgage repayment calculator
How to render millions of 2D objects smoothly with webgpu?

Pushgateway installation and Prometheus configuration

Solutions to problems in IE6 browser
随机推荐
Zabbix5.0.8-odbc monitoring Oracle11g
QT based software framework design
Native Crash的一切
SQL JOIN 入门使用示例学习左连接、内连接、自连接
6-16漏洞利用-rlogin最高权限登陆
手把手教你用 Power BI 实现 4 种可视化图表
QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content
How to render millions of 2D objects smoothly with webgpu?
Ah, I am uncle card space
Seckill implementation diagram
Is it safe for Huatai Securities to open a remote account? Is there any guarantee?
使用Jenkins搭建CI服务器
Wechat official account development: Material Management (temporary and permanent)
Unity rotation test
The biggest crisis for testers in the workplace is not at the age of 30, but being laid off in middle age
Use typeface to set the text font of textview
Acwing 92. recursive implementation of exponential enumeration
Okaleido tiger NFT is about to log in to binance NFT platform
How to upload pictures from typora to CSDN
雪花算法(PHP)