Semicolon after Function

后端 未结 6 1490
广开言路
广开言路 2021-02-18 12:57

Is there a specific reason why some people put a semicolon after the curly closing function bracket?

void foo() {

};
相关标签:
6条回答
  • 2021-02-18 13:23

    Just wanted to add my few opinions about semicolon insertion in general:

    1. As a matter of semantics .. semicolons after a function is okay if it is part of an executable statement like variable declaration for an anonymous function.

      var x = function(){ console.out(' Hello '); }

    2. Please be aware that if you add a semicolon after a function if un-necessary it will be counted as a statement in your LOC. Tools like Lint, Sonar etc will consider it as a Line of Code..Basically your client will be paying for the number of Lines of code.(like kloc metric ..Thousand lines of code was a common metric for billing your customers.)

    3. Also with semicolons, Javascript parser has a habit of inserting semicolons wherever it feels the current line's code might get broken like after a return statement in a function etc.The process of JS engine is called "Automatic Semicolon Insertion".You can google for Automatic semicolon insertion in ECMAScript standard.

    0 讨论(0)
  • 2021-02-18 13:28

    Not really, the semicolon there makes no difference. It's probably a matter of habit.

    You can put as many semicolons if you want though in C++11:

    void foo() {
    
    };;;;;;;;
    
    0 讨论(0)
  • 2021-02-18 13:29

    Why they do it is probably purely a matter of personal style (and a rather strange one, I'd add). But the legality of this is a completely different matter. In pre-C++14 versions of the language it depends on the scope in which this function definition is made.

    If you define such function in namespace scope, then the trailing ; has nothing to do with the function definition. In C++98 and C++03 it would simply be a syntax error. C++11 introduced (or "legalized") so called empty declarations in namespace scope (but not in class scope). So, that extra semicolon simply constitutes a separate empty declaration, which follows the function definition. This also means that in namespace scope you can add as many extra superfluous semicolons as you wish.

    If you define such function in class scope, then it is a different story. In all versions of the language (starting from C++98) you have always been allowed to add a single optional ; at the end of an in-class function definition. Such ; is an integral part of the function definition and it is explicitly allowed by the grammar. I.e. in class scope that trailing ; does not constitute an independent empty definition. This also means that in class scope you can only add one optional ; after function definition, but not more.

    However, in C++14 in order to resolve some issues caused by that optional ; in class method definitions C++14 re-designed this part of the grammar. Now the grammar for in-class member function definition no longer contains the aforementioned optional ;. Instead, starting from C++14 classes now support empty member declarations as well. So, the meaning of that redundant ; is now consistent across all scopes: it is just an independent empty declaration tacked on at the end of a function definition.

    So, to summarize the above with an example

    struct S
    {
      void foo()
        {};      // <- Legal and has always been legal
      void bar()
        {};;     // <- Legal starting from C++14, error before that
    };
    
    void baz()
    {
    };           // <- Legal starting from C++11, error before that
    
    0 讨论(0)
  • 2021-02-18 13:44

    The semicolon must follow the class definition curly closing bracket. It is not required after class member functions definitions inside of the class definition. It is required though after class member functions declarations alone inside of the class definition.

    #ifndef FRAME_COUNTER_H
    #define FRAME_COUNTER_H
    
    #include <iostream>
    
    #include <SDL/SDL.h>
    
    const Uint32 FPS = 60;
    const Uint32 DELAY_TIME = 1000.0f / FPS;
    
    
    class FrameCounter {
    
    public:
        FrameCounter();
    
        void setFPS(int FPS) { m_FPS = FPS / (m_frameAccumulator / 1000); }
    
        void start() { m_frameStart = SDL_GetTicks(); }
    
        void run();
    
        void reset() {}
    
        void print() { printf("\nFPS: %5.1f\n", m_FPS); }
    
    private:
        int m_frameNumber;
    
        Uint32 m_frameStart;
        Uint32 m_frameDuration;
        Uint32 m_frameAccumulator;
    
        double m_FPS;
    };
    
    #endif
    
    0 讨论(0)
  • 2021-02-18 13:47

    Nope, it is simply ignored. Looks like a typo.

    0 讨论(0)
  • 2021-02-18 13:48

    You have to ask the specific people. But if I had to guess: in C++ there are situations, when you have to put semicolon after }, like class, enum, struct - so maybe some people put it always, because they don't want to remember when it is necessary.

    0 讨论(0)
提交回复
热议问题