About #pragma once #ifndef and

[1] #pragma once the macro do?

In order to avoid the same header file is included multiple times, C / C ++ have (include) two kinds of macro implementations: one is #ifndef way; the other is the way #pragma once.
On the way to support these two compilers, and there is not much difference between the two. But the two still have some minor differences.

// Method 1: 
#ifndef __SOMEFILE_H__
 #define    __SOMEFILE_H__ 
... ... // declaration, defining statement 
#endif

// Second way: 
#pragma Once 
... ... // declaration, defining statement

What are the respective characteristics of both [3]?

(1)#ifndef

  #ifndef way by the C / C ++ language standards support. It not only ensures that the same file will not be included multiple times, but also to ensure the contents of two identical files (or snippet) will not be inadvertently contains.

  Of course, the downside is that if a different header file macro name accidentally "crash" might lead you to clearly see the header files exist, but the compiler can not find it hard to say the situation declared - a situation sometimes very people depressed.

  Since the compiler always need to open the header file in order to determine whether there are duplicate definitions, so when compiling large projects, ifndef will make the compilation time is relatively long, so some compilers gradually began to support #pragma once way.

(2)#pragma once

  #pragma once is generally guaranteed by the compiler: the same file will not be included more than once. Note that the term "the same file" means a file on a physical, rather than the same content two files.

  You can not make pragma once declared to a piece of code in the header file, but only for the file.

  The benefit is that you do not have to worry about the macro name conflict, of course, conflict-induced strange macro name does not appear. Compilation speed large-scale projects and thus improve a little.

  If a corresponding disadvantage is that multiple copies of the file header, the present method does not guarantee that they do not contain repeated. Of course, the problem "can not find the statement," compared to the macro name conflict-induced, this included more easily detected and corrected.

  In addition, this approach does not support cross-platform!

What is the connection between the two [4]?

 #pragma once produced in the following manner #ifndef, so many people may not even have heard of. Now it seems #ifndef more respected. Because #ifndef supported C / C ++ language standard, without any restrictions compiler;

 The #pragma once the way is not subject to some of the older versions of the compiler support, some support the compiler also intends to get rid of it, so it's possible compatibility is not good enough.

 In general, when a programmer to hear the case, will choose #ifndef way, to try to make your own code "live" longer periods of time, usually prefer to reduce the number of compiler performance, it is the programmer's personality, of course, this is a digression La.

 One use is to also see the two put together:


     Once #pragma
   #ifndef __SOMEFILE_H__
   #define __SOMEFILE_H__
   ... ... // declaration, defining statement
   #endif

  seems to want both the advantages of both. But as long as there is a risk to use the #ifndef macro name conflicts can not be avoided does not support #pragma once compiler error, so mix the two methods can not seem to bring more benefits, it touches make some people who are not familiar with Confused.
  Which method you choose, it should be understood in two ways under the circumstances, as the case may be. As long as there is a reasonable agreement to avoid the disadvantages, I think which way is acceptable. And this is not the standard compiler or responsibility should be to get the programmer or development of norms within a small range.

Reference from: https: //www.cnblogs.com/Braveliu/archive/2012/12/29/2838726.html

Guess you like

Origin www.cnblogs.com/zhaoyunt/p/11525262.html