且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问模板化基类的成员

更新时间:2023-02-15 15:27:35

" John Harrison" <乔************* @ hotmail.com>写道...
"John Harrison" <jo*************@hotmail.com> wrote...
此代码无法在Comeau C ++和VC ++ 7.1上编译(禁用语言
扩展名)

template< class T>
struct B
{
T b;
};

模板< class T>
结构D:B< T>
{
void f(){b = 1; }
};

int main()
{
D< int> x;
x.f();
}
错误消息引用''b = 1;'',消息''undefined identifier
b''。将此-b; b替换为b或将B替换为非模板类​​都可以编译
代码。

这里发生了什么?当我在VC ++上看到这个时,我感到非常惊讶,我认为这是一个编译器错误,但显然不是。
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.




为什么你说显然不&QUOT;?我在标准

中找不到任何会在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


"约翰哈里森 &LT;乔************* @ hotmail.com&GT;写道...
"John Harrison" <jo*************@hotmail.com> wrote...
此代码无法在Comeau C ++和VC ++ 7.1上编译(禁用语言
扩展名)

template< class T>
struct B
{
T b;
};

模板< class T>
结构D:B< T>
{
void f(){b = 1; }
};

int main()
{
D< int> x;
x.f();
}
错误消息引用''b = 1;'',消息''undefined identifier
b''。将此-b; b替换为b或将B替换为非模板类​​都可以编译
代码。

这里发生了什么?当我在VC ++上看到这个时,我感到非常惊讶,我认为这是一个编译器错误,但显然不是。
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.




为什么你说显然不&QUOT;?我在标准

中找不到任何会在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


On Sun ,2003年8月10日14:13:59 +0100,约翰哈里森, &LT;乔************* @ hotmail.com&GT;写道:
On Sun, 10 Aug 2003 14:13:59 +0100, "John Harrison" <jo*************@hotmail.com> wrote:
此代码无法在Comeau C ++和VC ++ 7.1上编译(禁用语言
扩展名)

template< class T> {
T b;
};

模板< class T>
结构D:B< T>
{
void f(){b = 1; }
};

int main()
{
D< int> x;
xf();
}
错误消息引用''b = 1;'',消息''undefined identifier b''。
将此-b; b替换为b或使B成为非模板类​​都可以编译代码。


我很高兴我甚至没有检查过我是否_风_ VC 7.1 ...... ;-)


编译得很好7.0,有或没有语言扩展

禁用(/ Za)。

这里发生了什么?当我在VC ++上看到这个时,我很惊讶我认为它是一个编译器错误,但显然不是。
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.
Am I glad that I haven''t even checked whether I _have_ VC 7.1... ;-)

It compiles just fine with 7.0, with and without language extensions
disabled (/Za).
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.




显然它是一个编译器错误。


至少,我同意Victor的观点,通常的查询规则应该适用。



Apparently it is a compiler bug.

At least, I agree with Victor that the usual look-up rules should apply.