Thread: Question about #pragma once

  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,336
    Only on MS compilers and only until MS breaks it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,895
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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, 07: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, 01:47 AM