The purpose of providing unnecessary semicolons in a class definition

Consider the following program:

#include <iostream>
struct Test
{
    int a;
    Test(int s) : a(s)
    { };        // Observe this semicolon
    int geta()
    {
        return a;
    }
};
int main()
{
    Test t(3);
    std::cout<<t.geta()<<'\n';
}

The program compiles fine even when I use the parameter in -pedantic-errorsboth gcc and clang. (See the demo here and here .) I also don’t get any error from the compiler if I put a semicolon at the end of the geta () function, as shown below:

int geta()
{
   return a;
}; // This also compiles fine without any error or warnings in both g++ & clang

So what is the purpose of resolving this unnecessary semicolon? Is there any use for this? Is this explicitly permitted by locale?

+4
source share
1 answer

A semicolon (;) means an empty declaration in C ++. You can see the Declarations

++ :

- -

: Class

, , ++. .

+7

Source: https://habr.com/ru/post/1615974/


All Articles