当前位置:网站首页>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 .

原网站

版权声明
本文为[Chasing young feather]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202161552070411.html