Is there a standard #include convention for C++?

前端 未结 8 1934
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 16:49

This is a rather basic question, but it\'s one that\'s bugged me for awhile.

My project has a bunch of .cpp (Implementation) and .hpp (Definition) files.

I f

8条回答
  •  醉话见心
    2021-01-01 17:23

    Check out John Lakos's Large-Scale C++ Software Design. Here's what I follow (written as an example):

    Interface

    // foo.h
    // 1) standard include guards.  DO NOT prefix with underscores.
    #ifndef PROJECT_FOO_H
    #define PROJECT_FOO_H
    
    // 2) include all dependencies necessary for compilation
    #include 
    
    // 3) prefer forward declaration to #include
    class Bar;
    class Baz;
    #include  // this STL way to forward-declare istream, ostream
    
    class Foo { ... };
    #endif
    

    Implementation

    // foo.cxx
    // 1) precompiled header, if your build environment supports it
    #include "stdafx.h"
    
    // 2) always include your own header file first
    #include "foo.h"
    
    // 3) include other project-local dependencies
    #include "bar.h"
    #include "baz.h"
    
    // 4) include third-party dependencies
    #include 
    #include 
    #include 
    #include 
    

    Precompiled Header

    // stdafx.h
    // 1) make this easy to disable, for testing
    #ifdef USE_PCH
    
    // 2) include all third-party dendencies.  Do not reference any project-local headers.
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #endif
    

提交回复
热议问题