Is #pragma once a safe include guard?

后端 未结 14 2635
青春惊慌失措
青春惊慌失措 2020-11-22 07:42

I\'ve read that there is some compiler optimization when using #pragma once which can result in faster compilation. I recognize that is non-standard, and thus

14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 08:13

    I don't know about any performance benefits but it certainly works. I use it in all my C++ projects (granted I am using the MS compiler). I find it to be more effective than using

    #ifndef HEADERNAME_H
    #define HEADERNAME_H
    ...
    #endif
    

    It does the same job and doesn't populate the preprocessor with additional macros.

    GCC supports #pragma once officially as of version 3.4.

提交回复
热议问题