Thread: MSVC #pragma once and inclusion guards

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    MSVC #pragma once and inclusion guards

    I was looking at the Source Engine SDK and noticed a header file that has #pragma once at the top followed by inclusion guards.

    I thought they did pretty much the same thing; is there any reason why they both were used or is it just superfluous?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Seems a complete waste of effort to me.

    Allegedly, it's slightly quicker to use the #pragma, but since it's non-portable you have to rely on the include guards at some point anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Salem
    Seems a complete waste of effort to me.

    Allegedly, it's slightly quicker to use the #pragma, but since it's non-portable you have to rely on the include guards at some point anyway.
    But this can be just a reason

    On compilers supporting this pragma - the file is not included at all after the first time
    On compilers that have no support - pragma is ignored, file is included, but it content is empty due to include guards on each include after the first
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    >> But this can be just a reason

    From what I can tell this is compiled for the XBox (haven't seen HL2 for the XBox in stores though) given the #ifdef _XBOX (etc) in various places.

    Perhaps the compiler for the XBox version doesn't have this pragma.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On VC++, using the standard include guards causes the preprocessor open the file, read and process its contents, and thus end up with an empty file on second inclusion.
    Using the pragma, it adds the path of the header to a list and doesn't even open it on the second time, leading to faster compilation.

    GCC, on the other hand, recognizes the include guard pattern and adds the header path to a list then, so it gets this benefit without the need for a proprietary pragma. (But GCC 4+ have full support for #pragma once anyway.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed