What is the difference between the role of pragma once and #ifndef?

Source: Internet
Author: User
Tags number sign

#pragma once
This is a more commonly used instruction, as long as the header file at the beginning of the addition of this command will ensure that the header file is compiled once
#pragma once is used to prevent a header file from being multiple include, #ifndef, #define, #endif用来防止某个宏被多次定义.
#pragma once is a compilation-related, that is, this compilation system can be used, but in other compiled systems may not be, that is, the transplant is poor, but now basically every compiler has this definition.
#ifndef, #define, #endif这个是C + + language-related, which is a macro definition in the C + + language that avoids multiple compilations of files through macro definitions. So it works on all compilers that support the C + + language, and it's best to use this approach if you're writing programs that cross-platform
#pragma
The language symbol string language symbol string is a character sequence that gives the unique compiler directives and parameters. The number sign (#) must be the first non-whitespace character in the line that contains the compilation instructions. Whitespace characters can be separated by number sign (#) and word pragma?.
Role:
To avoid the same file being include multiple times
1 #ifndef方式
2 #pragma once way
There is not much difference between the two compilers that can support both, but there are still some subtle differences.
Way One:
#ifndef __somefile_h__
#define __somefile_h__
...//Some statement statements
#endif
Way two:
#pragma once
...//Some statement statements
#ifndef的方式依赖于宏名字不能冲突, this will not only guarantee that the same file will not be included multiple times, but also ensure that two files with exactly the same content will not be included accidentally. Of course, the disadvantage is that if the macro name of the different header accidentally "crash", it may lead to the header file exists, the compiler insists that cannot find the state of the statement
#pragma once is guaranteed by the compiler: the same file will not be included more than once. Note that the "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 thinking about a macro name, and of course there's no strange problem with the macro name collision. 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, duplicate inclusions are more susceptible to discovery and remediation than the "no claims" issue caused by macro name collisions.
The way one is supported by the language so the portability is good, the way two can avoid name collisions

What is the difference between the role of pragma once and #ifndef?

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.