当前位置:网站首页>Static assertion static_ assert
Static assertion static_ assert
2022-06-22 16:17:00 【Ordinary people who like playing basketball】
List of articles
1. Assertion
Assertion (assertion) Is a common means of programming . In general , Assertion is to put a judgment expression whose return value always needs to be true in the statement , Used to exclude situations that should not occur logically in the design .
such as : A function always needs to input parameters within a certain range , Then the programmer can use assertions on this parameter , To force the program to exit when an exception occurs to this parameter , So as to avoid the program falling into logical confusion .
In some sense , Assertions are not necessary for normal programs , But for program debugging , Assertions often help program developers quickly locate program errors that violate certain prerequisites .
If we want to C++ Using assertions in programs , You need to include header files in the program or <assert.h>, The header file gives us assert macro , Used to assert at run time .
- eg:
#include <iostream>
#include <cassert>
using namespace std;
// Create a... Of a specified size char Type array
char* createArray(int size)
{
/* Assertion used assert(expression) , This is a macro , Its argument is an expression , This expression usually returns a Boolean value , And the expression must be true The program can continue to run down , Otherwise, it will be interrupted directly .*/
// Determine whether the array size is greater than by asserting 0
assert(size > 0); // Must be greater than 0, Otherwise, the program will be interrupted
char* array = new char[size];
return array;
}
int main()
{
char* buf = createArray(0);
// What is used here is vs Security functions provided , You can also use strcpy
strcpy_s(buf, 16, "hello, world!");
cout << "buf = " << buf << endl;
delete[]buf;
return 0;
}
- test : If createArray The parameter is less than or equal to 0, Program run to 16 Line exit directly , You will see the prompt message as shown in the following figure :

2. Static assertions
In the above example, we used assertions assert. but assert Is a runtime assertion , That is, it only works when the program is running .
Static assertions static_assert, Statics are assertions that can be checked at compile time , There is no need to reference the header file when using . Another benefit of static assertions is , You can customize the error message when the assertion is violated .
- Static assertions are very simple to use , It takes two parameters :
Parameters 1: Assertion expression , This expression usually needs to return a bool value
Parameters 2: Warning message , It is usually a string , In violation of the assertion ( Expression for false) Prompt this message when
Because based on VS The calculated byte size is different from the theoretical value , The following program is based on 64 position Linux To test , Use static assertions to verify that the current operating system is 32 position :
- eg:
// assert.cpp
#include <iostream>
using namespace std;
int main()
{
// The font reason looks like a =, Actually, these are two =
static_assert(sizeof(long) == 4, " error , No 32 Bit platform ...");
cout << "64bit Linux Pointer size : " << sizeof(char*) << endl;
cout << "64bit Linux long size : " << sizeof(long) << endl;
return 0;
}
- test :

The static assertion condition is judged as true, The program can continue to execute downward .
- Because the expression of static assertion is detected at the compilation stage , Therefore, the variable cannot appear in its expression , That is, the expression must be a constant expression .
3. 32 Bit system and 64 Comparison of data types of bit system :

- Reference resources : Static assertions static_assert
边栏推荐
- New design of databend SQL planner
- Simulation Implementation of string
- 让pycharm项目里面的文本模板支持jinjia2语法
- Jenkins automatically triggers compilation by checking code submissions
- pymssql模块使用指南
- 转:杰克·韦尔奇:战略就是要少点沉思,敏于行动
- 静态断言 static_assert
- Trust level of discover
- How to embody the value of knowledge management in business
- [译文] 弥合开源数据库和数据库业务之间的鸿沟
猜你喜欢

10款超牛Vim插件,爱不释手了

知识管理在业务中的价值如何体现
![[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want](/img/cb/826b3591bafa62cc71826732eb32ef.png)
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want

【山大会议】应用设置模块

在JFlash中添加未知类型的单片机

The odoo system sets priorities for the views independently developed by the original model

Linux安装mysql

Binary search (integer binary)

vector的模拟实现

SAP ABAP 中的用户出口和客户出口-015
随机推荐
School enterprise alliance is on the way! Huawei cloud gaussdb has come to universities again
知识管理在业务中的价值如何体现
Simulation of vector
【一起上水硕系列】Day Three - video
Differences between Oracle client and server
B树和B+树
Swift -- 保存打印日志到沙盒
Jenkins automatically triggers compilation by checking code submissions
Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
nvarchar和varchar的区别
着力打造网红产品,新捷途X70S焕新上市,8.79万起售
SAP价值流程&帮助请求流程-011
[Shangshui Shuo series] day three - VIDEO
[leetcode] 9. Palindromes
SLAM十四讲之第6讲--非线性优化
Cve-2022-0847 (privilege lifting kernel vulnerability)
ORB_ VI ideological framework
[VTK] model rotation and Translation
数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
Discover the number of media new users can insert