Naming Include Guards

前端 未结 9 1273
忘了有多久
忘了有多久 2020-11-29 08:37

How are C++ include guards typically named? I tend to see this a lot:

#ifndef FOO_H
#define FOO_H

// ...

#endif

However, I don\'t think

9条回答
  •  星月不相逢
    2020-11-29 09:05

    I personally follow Boost's recommendation. It's perhaps one of the largest collection of C++ libraries of good quality around and they don't have problem.

    It goes like:

    __...____INCLUDED
    
    // include/pet/project/file.hpp
    #ifndef PET_PROJECT_FILE_HPP_INCLUDED
    

    which is:

    • legal (note that beginning by _[A-Z] or containing __ is not)
    • easy to generate
    • guaranteed to be unique (as a include guard) within a project (else you have two files at the same place)
    • guaranteed not to be used for anything else (if you end another macro with INCLUDED you're spoiling for a fight)

    I've read about GUID but those look weird.

    And obviously I'd rather than all compilers implement #pragma once (or better, #pragma multiple and "once" be the default behavior...)

提交回复
热议问题