gpt4 book ai didi

c++ - 函数模板不能隐藏类名?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:20 26 4
gpt4 key购买 nike

这适用于 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 定义。差异仅仅是因为函数模板不是函数吗?我无法理解 GCC 的行为。

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++ - 函数模板不能隐藏类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5765924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com