Question about #pragma once

This is a discussion on Question about #pragma once within the C++ Programming forums, part of the General Programming Boards category; Normally I make a header file like this: Code: #ifndef HEADERFILE_H_ #define HEADERFILE_H_ ... #endif HEADERFILE_H_ However using the VS2008 ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    66

    Question about #pragma once

    Normally I make a header file like this:

    Code:
    #ifndef HEADERFILE_H_
    #define HEADERFILE_H_
    
    ...
    
    #endif HEADERFILE_H_
    However using the VS2008 Express IDE's Project->Add class wizard it does this:

    Code:
    #pragma once
    ...
    So my question is, does this do the same thing as the above code?
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Only on MS compilers and only until MS breaks it.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Yes.
    It has the added "bonus" feature of locking you into your compiler vendor.
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    It doesn't work reliably anyway. Use normal ifdef guards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    GCC undeprecated "#pragma once" since 3.4....so i wouldn't say it's vendor lock in.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Ok thanks, is there any way to make the VS compiler use the normal guards or do I just have to manually type it (not like I mind, but doing a project which is going to use a lot of classes so anything to make it faster is nice)
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    There's no built-in feature, though some 3rd party plugins may provide the means to do so.
    Or you could create a VS macro.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    ah ok thanks Elysia
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  9. #9
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    The #pragma once directive on MS compilers is said to speed up compilation in some situation. There's a difference between it and standard include guards. Being a pragma, there's not much to worry about portability since pragmas are ignored if the compiler don't understand them (well, you might have a warning message in those case).

    So it's not that much of a portability issue in my opinion. Nevertheless, I wouldn't consider using #pragma once in my code except in really special case.
    I hate real numbers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Being a pragma, there's not much to worry about portability
    The worry is that if you use it to the exclusion of guards, the code will fail to compile on compilers not supporting #pragma once.
    Also, the code becomes invalid under the rules of the C++ standard.

    As for speeding up compilation, it does, but only because the MS compiler still does not support the simple include guard recognition trick that pretty much every other compiler has supported for years and years.
    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    Quote Originally Posted by CornedBee
    Also, the code becomes invalid under the rules of the C++ standard.
    I believe that the code merely behaves in an implementation defined manner, which is not the same as saying that it is invalid as per the Standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Hmm.

    Possibly. Yeah, basically, if the code contains any pragma, anywhere, its entire semantics are completely implementation-defined.

    Not a very comforting thought.
    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

Similar Threads

  1. Programming a PIC 16F84 microcontroller in C
    By Iahim in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 02:23 PM
  2. Very Beginner Question
    By [Namuan] in forum C++ Programming
    Replies: 10
    Last Post: 01-09-2004, 06:59 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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