未命名的类 [英] Unnamed classes

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

问题描述

受到讨论的启发



C ++类的语法定义为



class-key identifier * [opt] * base-clause * [opt] * (斜体是我的)



这对我来说意味着类名是选项,我们可以在C ++中有未命名的类。



那么,以下是否是良好的?

  struct X {
struct {
int x;
int y;
};
};

int main(){}




VS:错误C2467:非法声明
匿名的'struct'



Comeau在线:错误:声明
不声明任何
struct {



GCC(ideone):编译正确


/ p>

解决方案

不,它的格式不正确。您不能从这些语法语句单独导出语言语法。在标准的文本中给出的额外要求也必须考虑。在这种情况下,这将是


7声明



...



3 在一个简单声明中,可选的 init-declarator-列表可以是
省略,只有当声明类
(第9节)或枚举(7.2)时,
是,当 decl-specifier-seq
包含 class-specifier
elaboratedtype-specifier
class-key (9.1)或枚举说明符

在这些情况下,只要
class-specifier enum-specifier
, seq
,这些说明符中的
标识符是由
声明声明的名称中的
(如 class-names
enum-names 枚举符,取决于语法上的
)。 在这种情况下,
除了声明
未命名位字段(9.6),
decl-specifier-seq
或更多的名字到程序中,或
将重新声明一个由
以前的声明引入的名称


最后一句是在这种情况下重要的一个



可选部分只允许像

  struct {int x; } s; 
typedef struct {int x,y; }点;

第一个声明没有链接和变量的类类型 s 。注意,没有链接的类型不能用于声明具有链接的变量,这意味着这样的声明不能在命名空间范围中使用。



您的示例是错误的,但这是合法的

  struct X {
struct {
int x;
int y;
} point;
};

此外,无名类语法用于声明匿名联合(虽然我有点困惑事实,7/3没有提到匿名工会)。


Motivated by the discussion

The grammar for C++ classes is defined as

class-key identifier *[opt]* base-clause *[opt]* (Italics are mine)

This to me means that the class name is option and we can have unnamed classes in C++.

So, is the following well-formed?

struct X{
   struct{
      int x;
      int y;
   };
};

int main(){}

VS: error C2467: illegal declaration of anonymous 'struct'

Comeau online: error: declaration does not declare anything struct{

GCC(ideone): Compiles fine

Any thoughts?

解决方案

No, it is not well-formed. You cannot derive the language syntax from these grammar statements alone. The extra requirements are given in the text of the standard also have to be taken into account. In this case that would be

7 Declarations

...

3 In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaboratedtype-specifier with a class-key (9.1), or an enum-specifier. In these cases and whenever a class-specifier or enum-specifier is present in the decl-specifier-seq, the identifiers in these specifiers are among the names being declared by the declaration (as class-names, enum-names, or enumerators, depending on the syntax). In such cases, and except for the declaration of an unnamed bit-field (9.6), the decl-specifier-seq shall introduce one or more names into the program, or shall redeclare a name introduced by a previous declaration.

The last sentence is the one that matters in this case

The "optional" part is only there to allow declarations like

struct { int x; } s;
typedef struct { int x, y; } Point;

The first one which declares a class type with no linkage and variable s of that type. Note that types with no linkage cannot be used to declare a variable with linkage, meaning that such declaration cannot be used in namespace scope.

Your example is ill-formed, but this would be legal

struct X {
   struct {
      int x;
      int y;
   } point;
};

Also, nameless class syntax is used to declare anonymous unions (although I'm a bit puzzled by the fact that 7/3 does not mention anonymous unions).

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

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