giacomo-b / CppRobotics

Header-only C++ library for robotics, control, and path planning algorithms. Work in progress, contributions are welcome!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't Use pragma once

erichlf opened this issue · comments

#pragma once isn't part of the c++ standard and isn't supported by all compilers, so I would suggest not using it.

#pragma once isn't part of the standard, but it is very well supported across compilers. As far as I know, most compilers have been supporting it for a while now.

Overall, since it is shorter (less code to type) and less error prone (eliminates the risk of name clashes), I think it is convenient to keep using #pragma once.

Intel's compilers work fine with #pragma once.
Intel had a warning that would show a diagnostic message when using this pragma -Wpragma-once, but that has been deprecated.
This old thread mentions that the pragma is NOT deprecated.
It's true that pragma once is sensitive to identical header names, but the include guards are in no way better (they have the same weakness).

That wiki article is wrong and here is an example of where it is wrong: https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/pragmas/intel-supported-pragma-reference.html

I believe @tomlankhorst is right. To be fair though, Intel's compiler doesn't even support all of C++17 features yet (this repo currently uses C++17).

Also, https://stackoverflow.com/a/34884735/1552338

It looks like some 18 year old bugs are being referenced in that post.

I may be biased, but the advantages I see for #pragma once are:

  • Less code to type
  • No name clashes
  • No global namespace pollution
  • No risk of spelling a macro wrong and/or forgetting to rename macros when moving files around

The only downside I see is that it may not be able to include the same file placed in multiple locations. But then, I don't know why the same file would be placed in multiple locations.

Given the several changes that will likely still occur to the project structure, I think #pragma once makes more sense for the time being. I am closing this for now, but we can discuss it more down the line if needed.