#pragma once and #ifndef #define ... #endif的区别

Source: Internet
Author: User

1.

#pragma once is used to prevent a head file from being include multiple times;

#ifndef, #define, #endif用来防止某个宏被多次定义.

2.

#pragma once is compile-related, which means it can be used on the compilation system, but not necessarily in other compiling systems, that is, poor portability, but now basically every compiler has this definition;

#ifndef, #define, #endif这个是C + + language-related, which is a macro definition in the C + + language, to avoid multiple compilations of files through a macro definition.   So it works on all compilers that support C + + languages, and it's best to use this approach if you're writing a program that spans the platform. 3. Different ways of use:

Mode one:

#ifndef __somefile_h__

#define __SOMEFILE_H__ ...

//some statements

#endif

Mode two:

#pragma once ...

//some statements

The way one is supported by the language so the transplant is good, the way two can avoid the name conflict (see 4th in detail)

4.

#ifndef的方式依赖于宏名字不能冲突, this will not only guarantee that the same file will not be included multiple times, but also that two files with exactly the same content will not be accidentally included. Of course, the disadvantage is that if the different headers of the macro name accidentally "Crash", may lead to the existence of the header file, but the compiler insists that can not find the state of the statement

#pragma once is guaranteed by the compiler: the same file will not be included multiple times. Note that the word "same file" here refers to a physical file, not two files of the same content. The advantage is that you don't have to bother to think of a macro name, of course, there will be no macro name collision caused by strange problems. 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, the repeated inclusion is more likely to be found and corrected than the "Can't find declaration" problem caused by the macro name collision.

#pragma once method is produced after #ifndef, so many people may not even have heard of it. At present, it seems #ifndef more respected. Because #ifndef is naturally supported by language and is not limited by the compiler, the #pragma once approach is not supported by older versions of the compiler, in other words, it's not good enough for compatibility.   Maybe, in a few years, when the old compiler dies, it's not a problem. PS: I also see a usage that puts the two together:

#pragma once
#ifndef __somefile_h__
#define __SOMEFILE_H__ ...
//some statements
#endif

 

It seems to be an advantage to have both. However, as long as the use of #ifndef there will be the danger of macro-name conflict, so mixing two methods does not seem to bring more benefits, but it will make some unfamiliar people confused.

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.