为什么不能直接定义匿名struct/class-es的模板化别名?
Why can templated aliases of anonymous struct/class-es not be defined directly?
我可以创建以下内容:
using Foo = struct { /*Implementation*/ };
template<class>
using Bar = Foo;
但是不允许以下内容:
template<class>
using Bar = struct { /*Implementation*/ };
来自 Clang 的错误比 GCC 更有帮助,并指出:
error: '(anonymous struct at file:line:column)' cannot be defined
in a type alias template
不允许第二个代码示例的任何原因?
注:
请说明第二个代码示例(如果允许)可能导致语言问题的任何示例。
标准中的任何引用也很有帮助。
定义 class 或 alias-declaration that is part of a template alias is forbidden by [dcl.typedef]/2 中的枚举:
A typedef-name
can also be introduced by an
alias-declaration.
...
The
defining-type-specifier-seq
of the
defining-type-id
shall not define a class or enumeration if the
alias-declaration
is the declaration of
a
template-declaration.
后者被介绍为CWG issue 1159 was accepted, as part of FCD N3092。
相关 N3092 comment US 74 的评论和提议的解决方案确实提供了一些关于为什么引入此限制的理由 [强调 我的]:
Comment (ID) US 74
Comment
An alias-declaration allows a class or enumeration type to be defined
in its type-id (7.1.6p3). However, it's not clear that this is
desirable when the alias-declaration is part of a template alias:
template<typename T> using A =
struct { void f(T) { } };
Proposed resolution
Either prohibit the definition of classes and enumerations in
template aliases, or prohibit the use of template parameters in such
definitions, or add an example illustrating this usage.
Owner & issue
Disposition
ACCEPTED
Definition of a class or enumeration is now prohibited in a template alias.
似乎没有人抗议(足够有说服力)禁止在模板别名中定义 classes 和枚举,这意味着很可能没有人能够给出一个令人信服的例子来说明在哪里这会很有用。
我可以创建以下内容:
using Foo = struct { /*Implementation*/ };
template<class>
using Bar = Foo;
但是不允许以下内容:
template<class>
using Bar = struct { /*Implementation*/ };
来自 Clang 的错误比 GCC 更有帮助,并指出:
error: '(anonymous struct at file:line:column)' cannot be defined in a type alias template
不允许第二个代码示例的任何原因?
注:
请说明第二个代码示例(如果允许)可能导致语言问题的任何示例。
标准中的任何引用也很有帮助。
定义 class 或 alias-declaration that is part of a template alias is forbidden by [dcl.typedef]/2 中的枚举:
A typedef-name can also be introduced by an alias-declaration.
...
The defining-type-specifier-seq of the defining-type-id shall not define a class or enumeration if the alias-declaration is the declaration of a template-declaration.
后者被介绍为CWG issue 1159 was accepted, as part of FCD N3092。
相关 N3092 comment US 74 的评论和提议的解决方案确实提供了一些关于为什么引入此限制的理由 [强调 我的]:
Comment (ID) US 74
Comment
An alias-declaration allows a class or enumeration type to be defined in its type-id (7.1.6p3). However, it's not clear that this is desirable when the alias-declaration is part of a template alias:
template<typename T> using A = struct { void f(T) { } };
Proposed resolution
Either prohibit the definition of classes and enumerations in template aliases, or prohibit the use of template parameters in such definitions, or add an example illustrating this usage.
Owner & issue
Disposition
ACCEPTED
Definition of a class or enumeration is now prohibited in a template alias.
似乎没有人抗议(足够有说服力)禁止在模板别名中定义 classes 和枚举,这意味着很可能没有人能够给出一个令人信服的例子来说明在哪里这会很有用。