Problem with inheritance and tamplates

D

David Crocker

The following won't compile under Comeau in strict mode, nor under MS
Whidbey beta in ANSI mode. Can anyone tell me why, and which sections of the
ISO standard apply? The error messages from Comeau are:return arg->foo() == bar() // line 45
^

"ComeauTest.c", line 43: error: identifier "Object" is undefined
&& Object
^

<<'bar()' is a public inherited member of the current class and 'Object' is
a protected inherited member of the current class. If I replace them by
this->bar() and this->Object, the errors go away. The source is:>>
#include <typeinfo> // RTTI header

class _eAnyBase
{
public :
_eAnyBase() {}

virtual const std::type_info& foo() const = 0;
virtual const std::type_info& bar() const = 0;

virtual bool _lEqual(const _eAnyBase* h) const = 0;
};

template <class X >
class _eWrapperBase : public _eAnyBase
{
protected:
X Object;
public :
_eWrapperBase(const X& x) : _eAnyBase (), Object(x) {}

const std::type_info& foo() const
{
return typeid(X);
}
const std::type_info& bar() const
{
return typeid(X);
}
};

template <class X >
class _eStorableWrapperBase : public _eWrapperBase<X >
{
public:
_eStorableWrapperBase(const X& x) : _eWrapperBase<X >(x)
{
}

bool _lEqual(const _eAnyBase *arg) const
{
return arg->foo() == bar()
&& Object
== static_cast<const _eStorableWrapperBase<X >* >(arg)->Object;
}
};

enum Enum2 {red, green, blue};

class _eWrapper : public _eStorableWrapperBase<Enum2>
{ // line 54
public:
_eWrapper(const Enum2 & x) : _eStorableWrapperBase<Enum2>(x) {}
};

<<

Thanks - David
 
S

Serock

Code has not any error in my file 'Test.cpp' under ms vc6 and ms
vs.net. I guess filename '.c' cause the error.
 
R

Rob Williscroft

David Crocker wrote in
in comp.lang.c++:
The following won't compile under Comeau in strict mode, nor under MS
Whidbey beta in ANSI mode. Can anyone tell me why,

I think this one is now in the FAQ, but anyway the name "bar" in
your code is a template argument dependant name, because its a member
of a base class that is dependent on a template argument.

You need to tell the compiler it is a member name, otherwise it
assumes that bar should be found in an outer scope.

This is part of Two Phase Lookup.

The error occurs when the template body is first parsed, at that time
the compiler doesn't know the template paramiter types, so it doesn't
know the type of the dependant base class or what members that base
class has.
and which sections
of the ISO standard apply?

I'd start at 14.6 (Templates / Name Resolution).
The error messages from Comeau are:
return arg->foo() == bar() // line 45
^

"ComeauTest.c", line 43: error: identifier "Object" is undefined
&& Object
^

<<'bar()' is a public inherited member of the current class and
'Object' is a protected inherited member of the current class. If I
replace them by this->bar() and this->Object, the errors go away.

HTH.

Rob.
 
S

Serock

I try to compile code under g++. Unfortunately, can't pass, until it is
modified below:

.......
bool _lEqual(const _eAnyBase *arg) const
{
return arg->foo() == _eWrapperBase said:
== static_cast<const _eStorableWrapperBase<X >* >(arg)->Object;
}
.......

( add '_eWrapperBase<X >::' )
 
D

David Crocker

Serock said:
Code has not any error in my file 'Test.cpp' under ms vc6 and ms
vs.net. I guess filename '.c' cause the error.

The error only happens if the compiler is run in strict ISO-conformance
mode. My original filename does end in ".cpp".

David
 
D

David Crocker

Rob, thanks for replying.
You need to tell the compiler it is a member name, otherwise it
assumes that bar should be found in an outer scope.

This is part of Two Phase Lookup.

The error occurs when the template body is first parsed, at that time
the compiler doesn't know the template paramiter types, so it doesn't
know the type of the dependant base class or what members that base
class has.
...
I'd start at 14.6 (Templates / Name Resolution).

Clause 14.6.2.3 of the standard does say that the base class scope is not
examined until the class template is instantiated, if the base class depends
on a template parameter. But what I am finding is that the base class scope
is not being examined at all, even when the base class is instantiated. [BTW
the Comeau compiler reports an error even if the template is not
instantiated; the MS compiler only reports the error when the template is
instantiated.]

I didn't find any reference to this problem in the FAQ.

David
 
D

David Crocker

Serock said:
I try to compile code under g++. Unfortunately, can't pass, until it is
modified below:

......
bool _lEqual(const _eAnyBase *arg) const
{

== static_cast<const _eStorableWrapperBase<X >* >(arg)->Object;
}
......

( add '_eWrapperBase<X >::' )

Which version of g++ is that? The original code from which this example was
extracted compiles successfully under g++ 3.3.1.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top