当前位置:网站首页>Pragma once Usage Summary
Pragma once Usage Summary
2022-06-27 16:06:00 【Chasing young feather】
1.#pragmaonce What does this macro do ?
In order to avoid the same header file being included (include) many times ,C/C++ There are two macro implementations in : One is #ifndef The way ; The other is #pragma once The way .
On the compiler which can support these two methods , There's not much difference between the two . But there are still some subtle differences between the two .
2. What is the difference between the two ways of use ?
The sample code is as follows :
Mode one :
#ifndef __SOMEFILE_H__
#define __SOMEFILE_H__
... ... // Statement 、 Definition statement
#endif
Mode two :
#pragmaonce
... ... // Statement 、 Definition statement
3. What are the characteristics of the two ?
(1)#ifndef
#ifndef The way is influenced by C/C++ Language standards support . It can not only ensure that the same file will not be included more than once , It can also guarantee two files with exactly the same content ( Or code snippets ) Will not be inadvertently included at the same time .
Of course , The disadvantage is that if the macro names in different header files are not careful “ Car crash ”, It may cause you to see that the header file exists , But the compiler insists that it can't find the declared state —— This situation is sometimes very depressing .
Because the compiler needs to open the header file every time to determine whether there are duplicate definitions , So when compiling large projects ,ifndef It will take a relatively long time to compile , So some compilers are starting to support #pragma once The way .
(2)#pragma once
#pragma once It is generally guaranteed by the compiler : The same file will not be included more than once . Pay attention to what's said here “ The same file ” It's a physical file , Not two files with the same content .
You can't do... On a piece of code in a header file pragma once Statement , And only for files .
The advantage is that , You don't have to worry about macro name conflict anymore , Of course, there will be no strange problems caused by macro name conflict . As a result, the compilation speed of large projects has also been improved .
The corresponding disadvantage is that if a header file has multiple copies , This method cannot guarantee that they are not included repeatedly . Of course , Compared with that caused by macro name conflict “ Statement not found ” The problem of , This repetitive inclusion is easy to find and fix .
in addition , This approach does not support cross platform !
4. What is the connection between the two ?
#pragma once The way comes from #ifndef after , So many people may not even have heard of . So far #ifndef More respected . because #ifndef suffer C/C++ Language standards support , No compiler restrictions ;
and #pragma once This method is not supported by some older compilers , Some supported compilers are going to get rid of it , So its compatibility may not be good enough .
generally speaking , When programmers hear that , Will choose #ifndef The way , In an effort to make your own code “ Survive ” Longer , Usually prefer to reduce some compilation performance , This is the personality of a programmer , Of course, this is a digression .
Also see a usage is to put the two together :
#pragma once
#ifndef __SOMEFILE_H__
#define __SOMEFILE_H__
... ... // Statement 、 Definition statement
#endif
summary :
It seems that I want to have both advantages . But as long as you use #ifndef There's a risk of macro name conflict , There is no way to avoid not supporting #pragma once The compiler reported an error , So mixing the two methods doesn't seem to bring more benefits , It will make some unfamiliar people confused .
Choose which way , It should be understood in two ways , As the case may be . As long as there is a reasonable agreement to avoid shortcomings , I think either way is acceptable . This is no longer the responsibility of the standard or compiler , It should be done by the programmer himself or by a small range of development specifications .
边栏推荐
- Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
- Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
- The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
- Openssf security plan: SBOM will drive software supply chain security
- ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA
- What is the level 3 password complexity of ISO? How often is it replaced?
- 鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- 特殊函数计算器
- 利用Redis实现订单30分钟自动取消
猜你喜欢
域名绑定动态IP最佳实践
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
List转Table
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
SQL parsing practice of Pisa proxy
LeetCode每日一练(主要元素)
保留有效位数;保留小数点后n位;
Bit. Store: long bear market, stable stacking products may become the main theme
【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
随机推荐
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
Difference between special invoice and ordinary invoice
开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
#27ES6的数值扩展
Taishan Office Technology Lecture: the first difficulty is vertical positioning
VS编译遇到的问题
【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
Numerical extension of 27es6
PSS: you are only two convolution layers away from the NMS free+ point | 2021 paper
Leetcode daily practice (main elements)
面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
List to table
事务的隔离级别详解
Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp
利用Redis实现订单30分钟自动取消
Li Chuang EDA learning notes 16: array copy and array distribution
洛谷入门1【顺序结构】题单题解
A distribution fission activity is more than just a circle of friends!
LeetCode每日一练(两数之和)