分号在类定义中 [英] Semicolons in a class definition

查看:227
本文介绍了分号在类定义中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读这篇文章,这里有一个代码示例:

I was reading this gotw and here's a code sample from there:

struct X {
  static bool f( int* p )
  {
    return p && 0[p] and not p[1:>>p[2];
  };
};

问题是:符合规范的编译器应该提供多少错误:

The question was: how many errors should a compliant compiler give:

我回答了一个,因为这段代码相当于

I answered one, because this code is equivalent to

struct X {
  static bool f( int* p )
  {
    return p && p[0] && ! p[1] > p[2];
  };
};

我在想一个静态函数定义后的分号是一个错误。但Sutter先生说0,并解释(除了我明白的事情)

And I was thinking that a semicolon after a static function definition would be an error. But Mr. Sutter says 0 and explains (apart from the things I do understand) that


extra分号是允许在
结束函数声明。

The "extra" semicolon is allowed at the end of a function declaration.

我的问题是:


  • 标准中的哪些文本允许这样做?

  • 这只是指会员功能吗?

  • 分号出现在两个成员之间或类定义中的任何其他位置,如

  • What text in the standard allows this?
  • Does this refer to member functions only?
  • Can a semicolon appear between two members or in any other place in a class definition as in

 struct X
 {
   int a;;;;int b; //Legal?
 };


推荐答案

是,在类说明符中的函数定义之后显式允许使用分号。因此,目前在C ++ 0x草案中,以下内容也是有效的:第一个分号属于函数定义,第二个分配给委托给函数定义非终端的类说明符。

Yes, a semicolon is explicitly allowed after a function definition in a class specifier. As a result, currently in the C++0x draft, the following is valid too: The first semicolon belongs to the function definition, the second to the class specifier delegating to the function-definition non-terminal.

struct A {
  void f() = delete;;
};

但是三个分号是非法的。在函数定义后有两个分号,有一个正文。规范中的相应文本是 9.2 [class.mem] 的语法。

But three semicolons would be illegal. As are two semicolons after a function definition having a body. The respective text in the spec is the grammar at 9.2[class.mem].

函数定义后的分号已经在C ++ 03中被允许,但在函数定义之后不允许在命名空间作用域。 C ++ 0x修复了通过引入空声明。但是只有当你在类体外的函数定义之后有一个分号时才会出现。

Semicolons after function definitons were allowed already in C++03, but they were not allowed at namespace scope after function definitions. C++0x fixes that by introducing empty-declarations. But those only appear when you have a semicolon after function definitions outside class bodies.

Sutter在函数声明的末尾介绍extra分号虽然,这是不完全正确。因为以下是无效的语法

Sutter talks about "extra" semicolons at the end of function declarations though, which is not entirely correct. Because the following is invalid syntax

struct A {
  void f();; // invalid!
};

类说明符中的额外分号仅在函数定义。此外,作为 9.2 的检查,它在函数定义是模板时无效

An extra semicolon in a class specifier is only valid after a function-definition. Also, as a checkup at 9.2 uncovers, it's not valid when the function definition is a template

struct A {
  template<typename T> void f() { }; // invalid!
};

这是因为它会被一个模板声明本身将剩余的文本解析为最终的函数定义),类说明符后面没有额外的;

This is because it is parsed by a template-declaration (which will itself parse the remaining text to function-definition eventually) for which the class specifier does not have an additional ; afterwards.

这篇关于分号在类定义中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆