非命名空间范围内的显式特化不会在 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 的输出:

This is GCC's output:

prog.cc:13:14: error: explicit specialization in non-namespace scope 'struct Widget<T>'
     template<>
              ^

我使用的是 GCC HEAD 8.0.1,带有 -std=c++2a.

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,[temp.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.

相关文章