Re: C++ error in template of template class
Re: C++ error in template of template class
- Subject: Re: C++ error in template of template class
- From: Steve Checkoway <email@hidden>
- Date: Wed, 28 Dec 2005 01:57:37 -0800
On Dec 28, 2005, at 12:05 AM, Scott Fraser wrote:
I was writing a template class of a template class, and was getting
some odd scoping errors, using Xcode 2.2 (gcc 4.??). CodeWarrior
(9.x) does not complain about this:
<snip>
Here's a shorter example exhibiting the same issue:
$ cat error2.cpp
struct foo { int i; };
template<typename T> struct bar : public foo { int j; };
template<typename T> struct baz : public bar<T>
{
baz() { i = 0; j = 0; }
};
$ g++ -Wall -c error2.cpp
error2.cpp: In constructor 'baz<T>::baz()':
error2.cpp:5: error: 'i' was not declared in this scope
error2.cpp:5: error: 'j' was not declared in this scope
I can avoid the two reported errors by adding the scope modifier,
"FirstTemplateClass<T2>::" in front of the two offending
references. But the question is, why does the compiler complain in
the first place??
I wish I knew too. Either it's a bug or the c++ standard explicitly
disallows this for some reason that doesn't spring to mind. Having
never found a free version of the standard, I can't even check it.
My guess is that when one has a template of a template, an
ambiguity has been introduced into the name resolution. I assume
it is similar to inheriting from a common ancestor class through
two different base classes. Was that the "BaseClassInt" of the
"FirstTemplateClass <xxx>" class, or the "BaseClassInt" of the
"FirstTemplateClass <yyy>" class? Both have a base class of
"BaseClass", but each instantiation of "FirstTemplateClass" has a
different "BaseClass", and the compiler can't tell which one
without an explicit scope modifier.
I don't think that is the case here since there is no double
inheritance since SecondTemplateClass<T> inherits from BaseClass only
though FirstTemplateClass<T>.
Also, the error message for ambiguity is quite a bit different:
$ cat error3.cpp
struct foo { int i; };
struct bar : public foo { };
struct baz : public foo, public bar
{
baz() { i = 0; }
};
e$ g++ -Wall -c error3.cpp
error3.cpp:4: warning: direct base 'foo' inaccessible in 'baz' due to
ambiguity
error3.cpp: In constructor 'baz::baz()':
error3.cpp:5: error: reference to 'i' is ambiguous
error3.cpp:1: error: candidates are: int foo::i
error3.cpp:1: error: int foo::i
Even those error messages seem wrong though. If I use bar::i in the
ctor, I get the error message:
error3.cpp:4: warning: direct base 'foo' inaccessible in 'baz' due to
ambiguity
You'll notice this is just a warning and it still compiles. I'm not
sure which i it sets though. If instead, I use the suggested foo:i, I
get:
error3.cpp:4: warning: direct base 'foo' inaccessible in 'baz' due to
ambiguity
error3.cpp: In constructor 'baz::baz()':
error3.cpp:5: error: 'foo' is an ambiguous base of 'baz'
(As expected, if I declare foo a public virtual base class of both
bar and baz, it works correctly.)
- Steve
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden