Thread: Check if #pragma once is supported at compile time?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Check if #pragma once is supported at compile time?

    Hi, is there any way to check if

    Code:
    #pragma once
    is supported by the compiler, during compile time?

    If it is supported I want to call it since it may speed up the compilation, but if it's not supported the line will generate a warning which I don't want to have.
    Come on, you can do it! b( ~_')

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to be portable, just use conventional header inclusion guards. Placing them at the very start of the header, before any introductory comments, would generally suffice for quick detection that a header has already been included.
    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

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Yes, I know, I use ordinary inclusion guards too but I still want to use pragma once if it is supported. They do so here in the boost bind library. However, they only use pragma once when the compiler is the Visual C++ compiler, but almost all large compilers supports this pragma directive and hence they miss out a lot of them.
    Come on, you can do it! b( ~_')

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TriKri
    However, they only use pragma once when the compiler is the Visual C++ compiler, but almost all large compilers supports this pragma directive and hence they miss out a lot of them.
    That may be because MSVC does not (did not?) support intelligent detection of header inclusion guards, so #pragma once would have been an optimisation for compiling. Furthermore, historically, even those compilers that did support #pragma once may have bugs in that support, e.g., because a header file may be included by different paths.

    Anyway, no: there is no way to check from within the code if #pragma once is supported (correctly). You have to either live with the warning, silence it, or list the compilers that you want to support and expand that list you saw in the Boost Bind header.
    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

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Okay, thanks for your answer. So compilation may not go that much faster with pragma once because many compilers have intelligent detection of header inclusion guards? That kind of makes sense, since the compiler I am using (qmake, Qt's copiler) supports pragma once but never uses it. Instead, it automatically wraps all newly created header files in ordinary inclusion guards. On the other hand, in that case it is strange that Boost bind still uses both. Ah well.
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile time
    By onako in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2011, 04:43 AM
  2. Getting compile time.
    By ThatDudeMan in forum C Programming
    Replies: 2
    Last Post: 10-13-2010, 12:15 PM
  3. Replies: 8
    Last Post: 06-03-2009, 03:00 PM
  4. Compile-Time Help
    By brayden37 in forum C++ Programming
    Replies: 0
    Last Post: 10-11-2001, 03:10 PM

Tags for this Thread