🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2129

Re: Why no constraint templates ?

#2129
From: Steve Clamage
Date: Thu, 05 Oct 1995 18:43
33 lines
1830 bytes
In article 11m3@melimelo.enst-bretagne.fr, croizier@rennes.enst-bretagne.fr (Valery Croizier) writes:
>Sometimes, I wish I could oblige the user of a template class to
>inherit from a given class: ...
>
> ...
>  o Is there a way I have not noticed to do this ?
>  o Does anybody know the reason why the team working on the
>standard rejected this feature ?

This subject is discussed in D&E, including why C++ templates do not
have an explicit constraint mechanism.

Your particular case can be handled like this:

template< class T > // T must be derived from Shape
class C {
	C() { static_cast<Shape*>(static_cast<T*>(0)); ... }
};

The constructor won't compile unless there is an implicit conversion from
T* to Shape*, meaning that T* must be or be derived from Shape. The
result of the casts isn't used, so no code should be generated. 

If your compiler doesn't support static_cast, you can write
	Shape* p = (T*)0; // requires implicit conversion from T* to Shape*
---
Steve Clamage, stephen.clamage@eng.sun.com



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <4517jj$h97@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <4510ou$11m3@melimelo.enst-bretagne.fr>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: clamage@Eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Why no constraint templates ?
Date: 5 Oct 1995 18:43:37 GMT
Organization: Sun Microsystems Inc.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <4517jj$h97@engnews2.Eng.Sun.COM>
References: <4510ou$11m3@melimelo.enst-bretagne.fr>
Reply-To: clamage@Eng
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 1021
X-Lines: 29
Originator: clamage@taumet