非命名空间范围内的显式专业化无法在GCC中进行编译 [英] Explicit specialization in non-namespace scope does not compile in GCC
问题描述
以下代码在Clang中编译,但不在GCC中编译:
The following code compiles in Clang but does not in GCC:
template<typename T>
struct Widget
{
template<typename U>
void foo(U)
{
}
template<>
void foo(int*)
{
}
};
根据C ++标准([temp.expl.spec],第2段):
According to the C++ standard ([temp.expl.spec], paragraph 2):
可以在可以使用相应主模板的任何范围内声明显式专业化 定义
An explicit specialization may be declared in any scope in which the corresponding primary template may be defined
这是GCC中的错误吗?如果是的话,如何在其错误跟踪器中找到它?
Is this a bug in GCC and if so how can I find it in its bug tracker?
这是GCC的输出:
prog.cc:13:14: error: explicit specialization in non-namespace scope 'struct Widget<T>'
template<>
^
我正在使用带有-std=c++2a
的GCC HEAD 8.0.1.
I'm using GCC HEAD 8.0.1, with -std=c++2a
.
推荐答案
这应该是GCC错误.完全专业化应允许在任何范围内使用,包括在类定义中使用.
This should be a GCC bug. Full specialization should be allowed in any scope, including in class definition.
根据 CWG 727 ,[临时expl.spec]第2段从
According to CWG 727, [temp.expl.spec] paragraph 2 was changed from
(重点是我的)
显式专门化必须在包含专门化模板的命名空间中声明.声明符-id或class-head-name不合格的显式专门化应在模板的最近封闭名称空间中声明,或者,如果该名称空间是内联(10.3.1 [namespace.def]),则应在其模板中声明任何名称空间.封闭名称空间集.这样的声明也可以是定义.如果声明不是定义,则可以稍后定义专门化(10.3.1.2 [namespace.memdef]).
An explicit specialization shall be declared in a namespace enclosing the specialized template. An explicit specialization whose declarator-id or class-head-name is not qualified shall be declared in the nearest enclosing namespace of the template, or, if the namespace is inline (10.3.1 [namespace.def]), any namespace from its enclosing namespace set. Such a declaration may also be a definition. If the declaration is not a definition, the specialization may be defined later (10.3.1.2 [namespace.memdef]).
到
(重点是我的)
可以在可以定义相应主模板的任何范围内声明显式专业化()(10.3.1.2 [namespace.memdef],12.2 [class.mem],17.6.2 [temp .mem]).
An explicit specialization may be declared in any scope in which the corresponding primary template may be defined (10.3.1.2 [namespace.memdef], 12.2 [class.mem], 17.6.2 [temp.mem]).
GCC似乎无法遵循.
It seems GCC fails to follow this.
编辑
我将此问题报告为错误85282 .
这篇关于非命名空间范围内的显式专业化无法在GCC中进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!