-
July 3rd, 2005, 02:28 PM
#1
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...
-
July 3rd, 2005, 04:14 PM
#2
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.
-
July 3rd, 2005, 04:20 PM
#3
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).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 3rd, 2005, 04:22 PM
#4
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.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
July 3rd, 2005, 04:28 PM
#5
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 4th, 2005, 04:42 AM
#6
Re: Is this legal?
Thanks for your comments, people.
So I just re-invented the wheel, it seems
-
July 4th, 2005, 04:54 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|