MSVC #pragma once and inclusion guards

This is a discussion on MSVC #pragma once and inclusion guards within the Tech Board forums, part of the Community Boards category; I was looking at the Source Engine SDK and noticed a header file that has #pragma once at the top ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,892
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21