user695652
user695652

Reputation: 4275

Semicolon after Function

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

void foo() {

};

Upvotes: 26

Views: 29601

Answers (5)

Rps
Rps

Reputation: 277

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

Upvotes: 2

smbear
smbear

Reputation: 1041

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.

Upvotes: 2

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

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

Upvotes: 8

Ed Swangren
Ed Swangren

Reputation: 124622

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

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

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() {

};;;;;;;;

Upvotes: 31

Related Questions