This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Template bug


Actually, it's easy in practice to force a template parameter to obey fairly
arbitrary constraints, including that a template parameter is derived from some
class.  For example:

class A {}; class B: public A {};
template<class T>
class foo {
  static void verifyderivation() { T* t=0; A* a=t; }
public:
#if CTOR_VERIFY
  foo() { verifyderivation(); }
  foo(const foo&) { verifyderivation(); }
#endif
#if DTOR_VERIFY
  ~foo() { verifyderivation(); }
#endif
};

main() {
  foo<A> fa;
  foo<B> fb;
  foo<int> fi;
}

The way this works is simple -- in order to actually use almost any class, one
of the constructors has to be called somewhere, by someone.  So if you just
insert a call to the verification function into all constructors, then you'll
get an error message whenever someone attempts to construct an object.  The
lazy man's way to perform verification is just to put it into the destructor. 
While it's only slightly less certain that the destructor of a class will be
used, it's a lot simpler than remembering to add a verifyderivation() call
whenever you add a new constructor.  The one added complication is that if you
have public static members or friend functions that are useful even when there
hasn't been a class object created, then you'll need to add a
verifyderivation() call to each them, also.
-- 
Ian Haggard  ||  ian@shellus.com (work)  ||  IanHaggard@juno.com (home)
GNU/Linux -- "Oh, no, Mr Bill!" || #define employer_opinion !my_opinion


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]