简体   繁体   中英

Best way to check multiple inclusions

#ifndef api_H_
#define api_H_
...
#endif

In above code it's checking that if file is already included or not to avoid multiple inclusions.

Question:

I want to know that if there is any better way to do so and what are the alternatives of it and if this is the better way of doing it then why it is so?

With most (if not all) modern compilers you can use #pragma once which does the same job, but which is possibly faster.

You also eliminate the risk of name collisions, which can happen if two different header files contain the same preprocessor symbol ( api_H_ ).

Basically instead of:

#ifndef api_H_
#define api_H_

... header content

#endif

you write:

#pragma once

... header content

Also read #pragma once vs include guards? and Is #pragma once a safe include guard?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM