当前位置:网站首页>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
边栏推荐
- 默认函数控制 =default 与 =delete
- 【华为云至简致远】征文获奖名单出炉!
- SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
- 数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
- 【山大会议】软件性能优化及bug修复
- MySQL trigger
- How to open a futures account? Is it safe to open an online futures account?
- SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
- Be an we media video blogger, and share the necessary 32 material websites
- 华为云HCDEZ专场暨分布式技术峰会:华为云分布式云原生技术与实践之路
猜你喜欢
![[Shanda conference] application setting module](/img/1e/1665234715b365614a753355274ced.png)
[Shanda conference] application setting module

Discover the number of media new users can insert

跨界融合创意创新,助力提高文旅夜游影响力

Scala language learning-05-a comparison of the efficiency of recursion and tail recursion

SLAM十四讲之第6讲--非线性优化

数睿数据受邀参与南通企业数字化转型研讨会

Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour

Binary search (integer binary)

知识管理在业务中的价值如何体现

Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
随机推荐
什么是 SAP ABAP? 类型、ABAP 完整形式和含义
Simulation of stack and queue
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
[Shanda conference] application setting module
ArcGIS JS之 4.23之IIS本地部署与问题解决
Quickly play ci/cd graphical choreography
Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest
[Shanda conference] use typescript to reconstruct the project
A simple understanding of hill ordering
CMake教程系列-00-简介
Merge sort of sorting
MySQL trigger
Google Chrome small details
SAP ABAP 表控制与示例-07
Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice
天翼云乘风新基建,构建数字化转型“4+2”能力体系
[leetcode] 9. Palindromes
String的模拟实现
跨界融合创意创新,助力提高文旅夜游影响力
【山大会议】WebRTC基础之用户媒体的获取