Topic: Derived classes as template args


Author: "glancaster" <glancaster@ntlworld.com>
Date: Sat, 19 May 2001 19:49:51 GMT
Raw View
Jake Lerner wrote:
[snipped example of self-parametised base class/curiously recurring template
pattern]
> > What does the standard say about this.

Edward Diener:
> It's fine and perfectly legal.

Agreed.

> The Microsoft Active Template Library for C++
> under Windows uses this idiom throughout.

Interestingly though the book "ATL Internals"
states that the VC++ compiler team promised they would keep it working even
though "the C++ standard does not mandate or prohibit such behavior". The
book concludes that it is unlikely to be portable.

Which raises the question of whether Microsoft understood that it was legal
when they started using it.

Kind regards

Garry Lancaster
Codemill Ltd
mailto << "glancaster" << at << "codemill" << dot << "net";
Visit our web site at http://www.codemill.net






---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Mark Radford <twonine@twonine.demon.co.uk>
Date: Wed, 9 May 2001 16:39:11 GMT
Raw View
In article <9c8pgk$269r$2@josh.sovintel.ru>, Igor Krasnopeev
<captaincomatoz@mail.ru> writes

>> template <class T>
>> class CBase
>> {
>> public:
>>      void Method1()
>>      {
>>           T Derived;
>>           Derived.Method2();
>>      }
>> };
>>
>> class CDerived : public CBase<CDerived>
>> {
>> public:
>>      void Method2()
>>      {
>>      }
>> };
[...]
>
>I'm not sure this will compile, cause when you use T Derived; CDerived is

This is completely legal.

>not implemented yet.

It does not matter. The construct in which T Derived occurs is not a
class, but a class *template*, and that is what makes the difference.
CBase itself is not a class, but instantiations of CBase are -
instantiation only occurs when the compiler comes across
CBase<Something>, like it does in CBase<CDerived>, but by this time such
uses of CDerived are legal.


--
Mark Radford

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Jake Lerner" <nospam@nospam.com>
Date: Wed, 25 Apr 2001 21:51:52 GMT
Raw View
Hi there,

Given the following:

template <class T>
class CBase
{
public:
     void Method1()
     {
          T Derived;
          Derived.Method2();
     }
};

class CDerived : public CBase<CDerived>
{
public:
     void Method2()
     {
     }
};

What does the standard say about this. I assume that since "CDerived" passes
itself as a template argument to its own base class, this isn't a problem
because the template argument T isn't evaluated until an object of class
"CDerived" is actually instantiated. That is, when "CDerived" is first
encountered by the compiler, it's passed as argument T to its own base class
but at that point "CDerived::Method2()" isn't known yet. Presumably this is
ok however since the call to that method in the base class isn't resolved
until a "CDerived" object is actually instantiated. By the time it is,
"CDerived" will then be fully known (in the nearest global or namespace
scope) so all is well. Is this the correct behavior? Thanks.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Igor Krasnopeev" <captaincomatoz@mail.ru>
Date: Thu, 26 Apr 2001 20:36:04 GMT
Raw View
Jake Lerner <nospam@nospam.com>
                           :%cGF6.56906$_P.15425074@news3.rdc1.on.home.com...
> Hi there,
>
> Given the following:
>
> template <class T>
> class CBase
> {
> public:
>      void Method1()
>      {
>           T Derived;
>           Derived.Method2();
>      }
> };
>
> class CDerived : public CBase<CDerived>
> {
> public:
>      void Method2()
>      {
>      }
> };
>
> What does the standard say about this. I assume that since "CDerived"
passes
> itself as a template argument to its own base class, this isn't a problem
> because the template argument T isn't evaluated until an object of class
> "CDerived" is actually instantiated. That is, when "CDerived" is first
> encountered by the compiler, it's passed as argument T to its own base
class
> but at that point "CDerived::Method2()" isn't known yet. Presumably this
is
> ok however since the call to that method in the base class isn't resolved
> until a "CDerived" object is actually instantiated. By the time it is,
> "CDerived" will then be fully known (in the nearest global or namespace
> scope) so all is well. Is this the correct behavior? Thanks.
>

I'm not sure this will compile, cause when you use T Derived; CDerived is
not implemented yet.
But I think T* Derived and Derived->Method2(); will work fine.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Edward Diener <eddielee@tropicsoft.com>
Date: Thu, 26 Apr 2001 22:11:46 GMT
Raw View
Jake Lerner wrote:

> Hi there,
>
> Given the following:
>
> template <class T>
> class CBase
> {
> public:
>      void Method1()
>      {
>           T Derived;
>           Derived.Method2();
>      }
> };
>
> class CDerived : public CBase<CDerived>
> {
> public:
>      void Method2()
>      {
>      }
> };
>
> What does the standard say about this.

It's fine and perfectly legal. The Microsoft Active Template Library for C++
under Windows uses this idiom throughout.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]