可以使用typedef名称来声明或定义构造函数吗? [英] Can typedef names be used to declare or define constructors?

查看:96
本文介绍了可以使用typedef名称来声明或定义构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[class.ctor] 12.1/1说

[class.ctor] 12.1/1 says

特殊的声明符语法用于声明或定义构造函数.语法使用:

A special declarator syntax is used to declare or define the constructor. The syntax uses:

    —可选的 decl-specifier-seq ,其中每个 decl-specifier 都是 function-specifier em>或 constexpr

    — an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,

   -构造函数的类名,和

    — the constructor’s class name, and

   -参数列表

    — a parameter list

.

[class.name] 9.1/4说

[class.name] 9.1/4 says

一个 typedef-name (7.1.3),该类命名一个类类型或具有cv资格的类 它的版本也是 class-name .如果 typedef-name 命名为 在需要 class-name 的情况下使用cv限定的类类型, cv限定词将被忽略. typedef-name 不得用作 类头中的标识符.

A typedef-name (7.1.3) that names a class type, or a cv-qualified version thereof, is also a class-name. If a typedef-name that names a cv-qualified class type is used where a class-name is required, the cv-qualifiers are ignored. A typedef-name shall not be used as the identifier in a class-head.

[expr.prim.general] 5.1.1/8说

Also [expr.prim.general] 5.1.1/8 says

使用 class-name :: class-name 的地方,并且两个class-names相互引用 对于同一个类,此符号命名为构造函数(12.1).

Where class-name :: class-name is used, and the two class-names refer to the same class, this notation names the constructor (12.1).


应用程序:

在我看来,这似乎应该允许使用typedef名称声明构造函数(尽管12.1/1并不使用斜体的 class-name ).


Application:

This seems to me to say that declaring a constructor should be allowed using typedef names (despite the fact that 12.1/1 doesn't use an italicized class-name).

例如,给定:

struct Foo;
typedef Foo Bar;

然后

struct Foo { Bar() {} }; // defines Foo's constructor. - 1

或给定

struct Foo;
struct Foo { Foo() };
typedef Foo Bar;

然后

Foo::Bar() {}; // defines Foo's constructor - 2

Bar::Bar() {}; // defines Foo's constructor - 3

Bar::Foo() {}; // defines Foo's constructor - 4

任何这些都应该合法.但是似乎没有人接受定义2或3,MSVC接受1,MSVC,clang和gcc都接受4.

Any of these should be legal. However nobody seems to accept definitions 2 or 3, MSVC accepts 1, and MSVC, clang, and gcc all accept 4.

我的分析正确吗,所有这些编译器都错了吗?

Is my analysis correct, and are all these compilers wrong?

推荐答案

在构造程序声明的 declarator-id 中,不得将 typedef-name 用作 class-name .

A typedef-name shall not be used as the class-name in the declarator-id for a constructor declaration.

这排除了(1).

§12.1/1似乎在声明和定义中都使用了声明"一词:

§12.1/1 seems to use the term "declaration" for both declarations and definitions:

特殊的声明符语法用于声明或定义构造函数. […]在这样的声明中,[…]

A special declarator syntax is used to declare or define the constructor. […] In such a declaration, […]

(未明确引用定义").我认为这是否适用于类外定义还是仅适用于内联定义尚不清楚.如果它适用于各种定义,则也将排除(2)和(3). (4)在任何情况下均应合法.

(without referring to "definitions" explicitly). I think it's a bit unclear whether this applies to out-of-class definitions or only to inline definitions. If it applies to all kinds of definitions, this would rule out (2) and (3) as well. (4) should be legal in any case.

这篇关于可以使用typedef名称来声明或定义构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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