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 05:58:08 -0800
On Dec 28, 2005, at 1:57 AM, Steve Checkoway wrote:
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:
<snip>
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.
Okay, I found free copy of the draft standard:
http://www.open-std.org/jtc1/sc22/wg21/docs/projects#14882
Section 14.6.2 "Dependent types" clause 3 says that this is illegal.
The simplest example of the code not working is:
template<typename T> struct bar { int j; };
template<typename T> struct baz : public bar<T>
{
baz() { j = 0; }
};
The above point reads:
In the definition of a class template or a member of a class
template, if a base class of the class template depends on a
template-parameter, the base class scope is not examined during
unqualified name lookup either at the point of definition of the
class template or member or during an instantiation of the class
template or member.
The example given is a bit strange but basically, "j = 0;" causes an
unqualified name lookup of j. You would think that the fully
qualified name should be baz<T>::bar<T>::j but that doesn't even
work. (I can't determine if that's a bug or not since it's very late
and the standard is a bit hard to read.) However, baz::j, baz<T>::j,
bar<T>::j or baz::bar::j all refer to the correct j.
bar::j does not work since it thinks that " 'template<class T> struct
bar' used without template parameters." The reason that baz::bar
works is that inside the template declaration (in this case the class
template baz's declaration), baz refers to baz<T> if the template
argument list is omitted and according to [1], we can use baz::bar to
refer to baz::bar<T>. But then, g++ chokes on "baz::bar<T>::j = 0" as
well (another bug?).
You really have to wonder if the standard committee decides
arbitrarily what is valid and what is not.
- Steve
[1] 14.6.1 clause 3:
The injected-class-name of a class template or class template
specialization can be used either with or without a template-
argument-list wherever it is in scope. [Example:
template <class T> struct Base {
Base* p;
};
template <class T> struct Derived: public Base<T> {
typename Derived::Base* p; // meaning Derived::Base<T>
};
--end example]
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