Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an #include before #ifdef/#define Include-Guard okay?

I always placed my #include after the #ifdef/#define Include-Guard. Now the refactor mechanism of my IDE (Qt Creator) put it before the Include-Guard e.g.

#include "AnotherHeader.h"

#ifndef MYHEADER_H
#define MYHEADER_H

Can this cause any problems or can I leave it this way?

like image 779
avb Avatar asked Jan 08 '14 06:01

avb


People also ask

What means of an?

1 : one that is of or relating to American. 2 : one skilled in or specializing in phonetician. -an. adjective suffix. variants: or -ian or less commonly -ean.

Whats the difference between an and A?

'A' and 'an' are both indefinite articles used before nouns or before adjectives that modify nouns. To determine if you should use 'a' or 'an' before a word, you need to listen to the sound the word begins with. Use 'a' if the word begins with a consonant sound and use 'an' if the word begins with a vowel sound.

What part of speech is the word an?

A noun is a word for a person, place, thing, or idea. Nouns are often used with an article (the, a, an), but not always. Proper nouns always start with a capital letter; common nouns do not.

What type of word is and?

Conjunction. A conjunction (also called a connective) is a word such as and, because, but, for, if, or, and when. Conjunctions are used to connect phrases, clauses, and sentences. The two main kinds are known as coordinating conjunctions and subordinating conjunctions.


3 Answers

Although less common, I believe this is considered to be acceptable, however beware: if you have circular #include's, your includes will typically include each other forever (until the pre-parser complains about the maximum include depth being reached). This will not happen with the #include's after the include guards.

(Circular #include's is not considered good style in any case, but if it is used nevertheless, it might work with #include's after the include guards, but certainly won't with the #include's before them.)

like image 63
Carl Avatar answered Sep 29 '22 15:09

Carl


Simply check out, what will happen. Let's assume, that two different headers uses MyHeader.h.

  1. AnotherHeader.h is included unconditionally
  2. Include guard allows loading rest of your header file.

The next time:

  1. AnotherHeader.h is included unconditionally again
  2. Include guard prevents from loading rest of your header file.

If AnotherHeader.h is include-guarded, nothing bad should happen. But generally I'd put the include-guard at the top of your file - there's no point in loading AnotherHeader.h again, when it was already loaded once.

like image 22
Spook Avatar answered Sep 29 '22 15:09

Spook


If the header in question has include guards itself, you won't run into problems. Putting it inside the include guards may still speed up compilation. Something the compiler does not see takes less time to compile, even if it does not produce any errors.

like image 27
nvoigt Avatar answered Sep 27 '22 15:09

nvoigt