-
Is this legal?
Hi folks!
I came around a construct that works, but is a bit strange, to put it mildly. I was wondering if it is legal. I didn't dive into the Standard -- I didn't have the time... Here it is:
Code:
template <typename T>
class A {/*...*/};
class B:public A<B> {/*...*/};
If you wonder what that's good for: 'A' implements common operations on a container which contains pointer-to-members of B. Sort fo hard to explain. I wasn't able to find a reason why this shouldn't work. OTOH it really doesn't look very healthy...
Looking forward to reding your comments...
-
Re: Is this legal?
I really think that it is legal.
B is implicitly partially declared like in a "class B;" statement when the compiler encounters "class B: ....", and a template can perfectly use a partially declared class if it just uses pointers to that class. Of course if the template uses class values (instead of pointers or references), it will generate a compiler error : "class B is not entirely defined at this position and its size is unknown" or something like that.
-
Re: Is this legal?
This is the "Curiously Recurring Template Pattern" (see here, for example).
This pattern is used quite extensively in Microsoft's ATL (or it was when I last used it about three years ago).
-
Re: Is this legal?
This is used extensively in ATL. The basic idea is to "aggregate functionality" without using virtual functions. The thing is that in the base class you'll define some functions in the following way:
Code:
void A::func(int param)
{
// Do something
// Now we need to call a "compile-time virtual function"
T *p = static_cast<T *>(this);
p->somefunction(param);
}
There is an article by Zeeshan here which goes into a bit more depth.
Basically it's a bit faster than runtime polymorphism as used in MFC for example. I've used ATL so much that I don't think it's unhealthy but I can understand why you find it strange.
-
Re: Is this legal?
Another example might be to implement intrusive reference counting:
Code:
template <class T>
class refcounted
{
protected:
friend class SmartPointer<T>;
void AddRef() { ++count_; }
void Release()
{
if (--refcount == 0)
delete this;
}
refcounted() : count_(0) {}
virtual ~refcounted() {}
private:
int count_;
};
class myclass : public refcounted<myclass> {}
This allows SmartPointer or the derived class to access AddRef and Release, but not any other types. Note also that SmartPointer<U> can't muck about with a T, either.
-
Re: Is this legal?
Thanks for your comments, people.
So I just re-invented the wheel, it seems :)
-
Re: Is this legal?
And it's fine when T is always a full concrete class.
When T is part of a hierarchy, you can get a problem as an A<Derived> is not derived from an A<Base>, but your class will derive separately from both and you may get ambiguities.
You might also decide to create a complete base thus ABase from which all the A<T> derive. Now it's true that A<Derived> and A<Base> both derive from ABase but you have the inheritance diamond, so you'll need to derive A<T> virtual from ABase.