The use of #pragma once in C + +

Source: Internet
Author: User

In C + +, there are two ways to avoid the same file being included multiple times: One is the #ifndef method, the other is the #pragma once method (at the beginning of the header file).

#ifndef Some_unique_name_here #define Some_unique_name_here//  contents of the header... #endif // Some_unique_name_here
#pragma once//  contents of the header...

#ifndef的是方式是受C/c++ language Standard support. #ifndef方式依赖于宏名不能冲突. It does not only guarantee that the same file will not be included more than once, but also ensure that two files with exactly the same content will not be included accidentally. The downside is that if a macro name in a different header is accidentally "colliding", it may cause you to see that the header file exists, and the compiler insists it cannot find the state of the statement. Because the compiler needs to open the header file each time to determine whether there are duplicate definitions, #ifndef会使得编译时间相对较长 when compiling large projects, so some compilers gradually start to support #pragma once.

#pragma once is generally guaranteed by the compiler: the same file will not be included multiple times. The word "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. This way there is no strange problem with the macro name collision, so the compilation speed of the large project also provides some. The disadvantage is that if there are multiple copies of a header file, this method does not guarantee that they will not be included repeatedly. The #pragma once is a non-standard but widely supported way in C/s + + .

#pragma once mode is generated after #ifndef. #ifndef方式受C the support of the/c++ language standard, which is not limited by the compiler, and #pragma once way some compilers do not support (older compilers do not support, such as the GCC 3.4 version before #pragmaonce), compatibility is not good enough . #ifndef可以针对一个文件中的部分代码, and #pragma once only for the entire file.

The use of #pragma once in C + +

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.