principlecppCritical
#pragma once vs include guards?
Viewed 0 times
onceincludepragmaguards
Problem
I'm working on a codebase that is known to only run on windows and be compiled under Visual Studio (it integrates tightly with excel so it's not going anywhere). I'm wondering if I should go with the traditional include guards or use
Note: to get the faster compile times we could use Redundant Include Guards but that adds a tight coupling between the included file and the including file. Usually it's ok because the guard should be based on the file name and would only change if you needed to change in the include name anyways.
#pragma once for our code. I would think letting the compiler deal with #pragma once will yield faster compiles and is less error prone when copying and pasting. It is also slightly less ugly ;)Note: to get the faster compile times we could use Redundant Include Guards but that adds a tight coupling between the included file and the including file. Usually it's ok because the guard should be based on the file name and would only change if you needed to change in the include name anyways.
Solution
I don't think it will make a significant difference in compile time but
To speed up compile time more just forward declare instead of including in .h files when you can.
I prefer to use
See this wikipedia article about the possibility of using both.
#pragma once is very well supported across compilers but not actually part of the standard. The preprocessor may be a little faster with it as it is more simple to understand your exact intent.#pragma once is less prone to making mistakes and it is less code to type.To speed up compile time more just forward declare instead of including in .h files when you can.
I prefer to use
#pragma once.See this wikipedia article about the possibility of using both.
Context
Stack Overflow Q#1143936, score: 449
Revisions (0)
No revisions yet.