0

Is it bad practice to put #endif at the beginning of a c++ header file after #ifndef and #define? If so, why?

This question does not touch on why #endif is at the end - which is specifically what i was googling for.

For example

//cDate.h
#ifndef CDATE_H_EXISTS
#define CDATE_H_EXISTS

#include <string>


class cDate {
  private:
    std::string day;
    std::string month;
    std::string year;
  public:
    void setDate(std::string, std::string, std::string);
    std::string getDate(int);
}; // end class def

#endif

vs

 //cDate.h
    #ifndef CDATE_H_EXISTS
    #define CDATE_H_EXISTS
    #endif

    #include <string>


    class cDate {
      private:
        std::string day;
        std::string month;
        std::string year;
      public:
        void setDate(std::string, std::string, std::string);
        std::string getDate(int);
    }; // end class def
Ulad Kasach
  • 7,372
  • 7
  • 41
  • 67
  • 2
    Ask yourself what the purpose of `#ifndef CDATE_H_EXISTS` is... – M.M Feb 19 '16 at 01:21
  • Putting `#endif` at the beginning of a c++ header file defeats the entire purpose of the `#ifdef` preprocessor directive: to avoid the header's contents being included multiple times by the same source file. – Emile Cormier Feb 19 '16 at 01:22
  • 4
    Possible duplicate of [Why are #ifndef and #define used in c++ header files](http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files) – Emile Cormier Feb 19 '16 at 01:24
  • @EmileCormier I argue it is not a duplicate due to the above question asking why use them, and this question asking why is #endif specifically at the bottom. For some reason before these answers I thought that when the compiler saw it was defined it did not compile the rest of the page, as opposed to just the inside if the #ifndef – Ulad Kasach Feb 19 '16 at 01:29
  • 1
    @VladK, ok fair enough. I'll retract my close vote. – Emile Cormier Feb 19 '16 at 01:46
  • 1
    @VladK: It is a duplicate. The `#endif` towards the end of the header file is just part of the include guard. That's like asking why C++ mandates that opening braces must be matched with closing braces. – IInspectable Feb 19 '16 at 01:48

4 Answers4

7

First example, it is include guard

Second example, it is nothing-guard.

Trevor
  • 5,074
  • 2
  • 16
  • 42
3

It doesn't accomplish its task; including this header file again will include all the lines again.

Consider the if statements:

if (condition) {
    <do something>
}

vs.

if (condition) {
    (void) 0;
}
<do something>

The condition is the #ifndef (if not defined), the opening brace is the initial condition, and the closing brace is the #endif.

russianfool
  • 332
  • 2
  • 12
1

You need to exclude the full contents of the file if it's already processed.

The purpose is not to #define the header guard - that's a useful(ish) side effect. The purpose is to ensure the header content is loading only once.

Xiong Chiamiov
  • 11,582
  • 8
  • 55
  • 95
pm100
  • 32,399
  • 19
  • 69
  • 124
1

While, subtle, the difference is huge. Assuming you were to place the #endif right after your #define statement at the top, that definition would be completely blank, and if there is a situation where your header file is being included more than once, the file will still be compiled and cause issues because nothing was defined.

Once the flip side, the #endif is supposed to be at the very end to set the definition as the ENTIRE file and its code contents, so that if it is included more than once, none of the file will be executed in excess.

m_callens
  • 4,800
  • 4
  • 24
  • 42