#pragram Once and #ifndef identification

Source: Internet
Author: User

"Turn" http://www.cnblogs.com/Braveliu/archive/2012/12/29/2838726.html

"1" #pragma once what is the role of this macro?

In order to avoid the same file being include multiple times, there are two kinds of macro implementations in C/s: One is #ifndef mode, the other is #pragma once way.
On compilers that can support both, there is not much difference between the two, but there are still some subtle differences.

What is the difference between "2" and how to use them?

The sample code is as follows:

1//Way one: 2 #ifndef   __somefile_h__3 #define   __somefile_h__4 ...//Declaration, definition Statement 5 #endif6 7//Way two: 8 #pragma once9 ... ...//Declaration, definition statement

What are the characteristics of the "3"?

(1) #ifndef

#ifndef的方式受C/c++ language Standard support. It not only guarantees that the same file will not be included multiple times, but also that two files (or snippets) with exactly the same content will not be included accidentally.

The downside, of course, is that if you accidentally "crash" a macro name in a different header file, it might cause you to see that the header file exists, but the compiler insists it can't find the state of the statement-a situation that is sometimes frustrating.

Because the compiler needs to open the header file each time to determine if there is a duplicate definition, IFNDEF will make the compilation time relative when compiling a large project

Long, so some compilers gradually begin to support the #pragma once approach.

(2) #pragma once

#pragma once is generally guaranteed by the compiler: the same file will not be included multiple times. Note that the "same file" here refers to a physical file, not two files of the same content.

You cannot make a pragma once declaration for a piece of code in a header file, but only for a file.

The advantage is that you no longer have to worry about macro name collisions, and of course there is no strange problem with macro name collisions. The compilation speed of large projects has thus improved a bit.

The disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. Of course, compared to the macro name conflict, the "Can't find

To the statement "of the problem, this repetition contains very easy to be discovered and corrected.

What is the connection between the "4"?

#pragma once way is generated after #ifndef, so many people may not even have heard of it. Now it seems that #ifndef is more admired. Because #ifndef is supported by the C + + language standard, it is not limited by the compiler;

The #pragma once approach is not supported by older versions of compilers, and some supported compilers intend to remove it, so compatibility may not be good enough.

Generally speaking, when the programmer hears this, will choose the #ifndef way, in order to try to make their code "live" longer, usually prefer to lower some of the compiler performance, this is the programmer's personality, of course, this is a digression.

Also see a usage that puts the two together:

#pragma once
#ifndef __somefile_h__
#define __somefile_h__
...//Declaration, definition statement
#endif

It seems to have the advantage of both. However, as long as the use of #ifndef will be the danger of macro name conflict, and can not avoid support #pragma once compiler error, so mixing two methods does not seem to bring

More of the benefits, it will make some unfamiliar people feel confused.

Which method to choose, should be understood in two ways, depending on the situation. As long as there is a reasonable agreement to avoid the shortcomings, I think which kind of side

The formula is acceptable. This is not the standard or compiler responsibility, should be the programmer himself or a small scope of development norms to be done.

#pragram Once and #ifndef identification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.