问题描述
此代码无法在Comeau C ++和VC ++ 7.1上编译(禁用语言
扩展名)
模板< class T>
struct B
{
T b;
};
template< ; T类>
结构D:B< T>
{
void f(){b = 1; }
};
int main()
{
D< int> x;
xf();
}
错误消息引用''b = 1;''和消息''未定义标识符b''。
将此 - > b替换为b或使B成为非模板类都会使
代码编译。
这里发生了什么?当我在VC ++上看到这个时,我感到非常惊讶,我认为这是一个编译器错误,但显然不是。
john
This code fails to compile on Comeau C++ and VC++ 7.1 (with language
extensions disabled)
template <class T>
struct B
{
T b;
};
template <class T>
struct D : B<T>
{
void f() { b = 1; }
};
int main()
{
D<int> x;
x.f();
}
Error messages refer to ''b = 1;'' with the message ''undefined identifier b''.
Substituting this->b for b or making B a non-template class both make the
code compile.
What''s going on here? I was so surprised when I saw this on VC++ that I
assumed it was a compiler bug, but apparently not.
john
推荐答案
为什么你说显然不"?我在标准
中找不到任何会在D< T> :: f()中查找名称b的内容失败。至于
标准,应该适用10.2的通常规则。如果在D定义中找不到''b'',则基类有待搜索
。
14.6 / 8说寻找时声明模板中使用的名称
定义,通常的查找规则(3.4.1,3.4.2)用于非独立的
名称。
3.4.1 / 8表示X的成员函数定义中使用的名称或X的
函数(9.3)29)以下之一
方式:
- 在使用它之前或在封闭区块中使用之前
(6.3 )或者
- 应该是X类成员或者是X(10.2)基类的成员,
或......
因此,应该是X类成员或者是X基类的成员
应该这样做。 ''b''是基类的成员。
好吧,至少那是我读它的方式。也许Greg Comeau或其他知识渊博的人会启发我们两个。
Victor
Why do you say "apparently not"? I cannot find anything in the Standard
that would make the look-up of name ''b'' in D<T>::f() fail. As far as the
Standard goes, the usual rules of 10.2 should apply. The base class has
to be searched if ''b'' is not found in D definition.
14.6/8 says "When looking for the declaration of a name used in a template
definition, the usual lookup rules (3.4.1, 3.4.2) are used for nondependent
names."
3.4.1/8 says "A name used in the definition of a function that is a member
function (9.3)29) of class X shall be declared in one of the following
ways:
- before its use in the block in which it is used or in an enclosing block
(6.3), or
- shall be a member of class X or be a member of a base class of X (10.2),
or ..."
So, "shall be a member of class X or be a member of a base class of X"
should do it. ''b'' is a member of the base class.
Well, at least that''s how I read it. Perhaps Greg Comeau or other
knowledgeable people will enlighten us both.
Victor
为什么你说显然不"?我在标准
中找不到任何会在D< T> :: f()中查找名称b的内容失败。至于
标准,应该适用10.2的通常规则。如果在D定义中找不到''b'',则基类有待搜索
。
14.6 / 8说寻找时声明模板中使用的名称
定义,通常的查找规则(3.4.1,3.4.2)用于非独立的
名称。
3.4.1 / 8表示X的成员函数定义中使用的名称或X的
函数(9.3)29)以下之一
方式:
- 在使用它之前或在封闭区块中使用之前
(6.3 )或者
- 应该是X类成员或者是X(10.2)基类的成员,
或......
因此,应该是X类成员或者是X基类的成员
应该这样做。 ''b''是基类的成员。
好吧,至少那是我读它的方式。也许Greg Comeau或其他知识渊博的人会启发我们两个。
Victor
Why do you say "apparently not"? I cannot find anything in the Standard
that would make the look-up of name ''b'' in D<T>::f() fail. As far as the
Standard goes, the usual rules of 10.2 should apply. The base class has
to be searched if ''b'' is not found in D definition.
14.6/8 says "When looking for the declaration of a name used in a template
definition, the usual lookup rules (3.4.1, 3.4.2) are used for nondependent
names."
3.4.1/8 says "A name used in the definition of a function that is a member
function (9.3)29) of class X shall be declared in one of the following
ways:
- before its use in the block in which it is used or in an enclosing block
(6.3), or
- shall be a member of class X or be a member of a base class of X (10.2),
or ..."
So, "shall be a member of class X or be a member of a base class of X"
should do it. ''b'' is a member of the base class.
Well, at least that''s how I read it. Perhaps Greg Comeau or other
knowledgeable people will enlighten us both.
Victor
显然它是一个编译器错误。
至少,我同意Victor的观点,通常的查询规则应该适用。
Apparently it is a compiler bug.
At least, I agree with Victor that the usual look-up rules should apply.
这篇关于访问模板化基类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!