c – 函数模板无法隐藏类名?

这适用于GCC和Comeau:

struct X {};

void X() {}

这在Comeau打破了:

struct X {};

template< typename T >
void X() {}

这打破了两个:

template< typename T >
struct X {};

template< typename T >
void X() {}

该规则由§3.3.7/ 2定义.差异只是因为函数模板不是函数吗?我无法理解海湾合作委员会的行为.

A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.

最佳答案
这是因为规范在14.p5中说:

A class template shall not have the same name as any other template, class, function, variable, enumeration,
enumerator, namespace, or type in the same scope (3.3), except as specified in (14.5.5). Except that a
function template can be overloaded either by (non-template) functions with the same name or by other
function templates with the same name (14.8.3), a template name declared in namespace scope or in class
scope shall be unique in that scope.

转载注明原文:c – 函数模板无法隐藏类名? - 代码日志