問題描述
以下代碼在 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.
這篇關于非命名空間范圍內的顯式特化不會在 GCC 中編譯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!