当前位置:网站首页>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
边栏推荐
- Applet development - Custom expiration cache
- jmeter关联登录302类型的接口
- Differences between Oracle client and server
- DDD understanding of Domain Driven Design
- 数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
- 84. (cesium chapter) movement of cesium model on terrain
- 10款超牛Vim插件,爱不释手了
- 跨界融合创意创新,助力提高文旅夜游影响力
- 【山大会议】注册页的编写
- [leetcode] 9. Palindromes
猜你喜欢

SAP ABAP BAPI-016

84. (cesium chapter) movement of cesium model on terrain

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

C language learning -18-makefile file writing examples and how to generate and call dynamic libraries

Process address space

mysql - sql执行过程

jmeter关联登录302类型的接口

Jenkins 通过检查代码提交自动触发编译

C language learning -17- function is passed in as a parameter

SAP ABAP 中的用户出口和客户出口-015
随机推荐
Scala language learning-04-function passed in as parameter function as return value
POD 类型
Solve the problem of MySQL remote login permission
Binary search (integer binary)
数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
Batch export excel zip using zipfile, openpyxl and flask
【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
[leetcode] 9. Palindromes
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
【山大会议】项目初始化
Jenkins 通过检查代码提交自动触发编译
天翼云乘风新基建,构建数字化转型“4+2”能力体系
什么是 SAP ABAP? 类型、ABAP 完整形式和含义
Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest
实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
Merge sort of sorting
Navicat premium connecting to Oracle database (Graphic tutorial)
[Shanda conference] private chat channel webrtc tools
Uni develops wechat applet to customize automatic camera detection (portrait + ID card)