可以使用typedef名称来声明或定义构造函数吗? [英] Can typedef names be used to declare or define constructors?
问题描述
[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?