Article View: comp.std.c++
Article #2217Re: Determining between classes and built-in t
From: Alex Martelli
Date: Mon, 16 Oct 1995 15:04
Date: Mon, 16 Oct 1995 15:04
85 lines
4183 bytes
4183 bytes
horstman@jupiter.SJSU.EDU (Cay S. Horstmann) writes:
...
>>>Is there a way to identify whether a data type in a template function is a
>>>class or a built-in type? For example,
...
>I did have this problem when trying to write a decent array template.
>For numeric and pointer types, I'd like to use memmove when the array
>grows and is relocated. For class types that cannot be done in
>general. I did "solve" the problem by having two templates, but now
Ah, so you _don't_ really care whether a certain type is built-in or
not -- what you REALLY want to know is whether an object of that type
can be moved via a simple memmove, or whether it needs the more
accurate process of "copy constructing to the new place" [presumably
via a placement new] followed by "destructing in the old place".
So, ask for _that_... have a "template<class T> bool can_memmove()"
that returns false by default, specialize it to return true for types
that CAN be moved by memmove() (all built-ins, but also several
non-built-ins, such as complex), and further allow the user of your
library to specialize it that way for _their_ own memmove'able types.
In your array class, do a "runtime" test of can_memmove() [any decent
compiler will presumably optimize away the half of your code that is
compile-time unreachable for any given instantiation of your template].
The only problem, as usual, is with _pointers_, since you cannot
specialize a template for "pointers to *anything*", alas:-(. Now
THAT would be a neat little thing indeed to have -- a rule allowing
a template to co-exist for both "any class" AND "any pointer", with
the latter taking precedence if both versions exist and the template
is instantiated for a pointer...
Actually, given that auto_ptr and similar "smart-pointer" templates
should be "taking over" from raw pointers in user code as time goes
by, just as useful would be the ability to specialize something like...:
--cut wishfulthinking.cc
#include <iostream.h>
#include <auto_ptr>
template<class T> inline bool isptr(const T&) { return false; }
template<class T> inline bool isptr(const T*&) { return true; }
template<class T> inline bool isptr(const auto_ptr<T>&) { return true; }
// user can add their own smartpointer templates...:
template<class T> class ptr { // other hypothetical smartpointer template
private: T*it;
public: // pointerlike accessors/mutators to taste
};
// ...AND specialize isptr() to make it recognize them...:
template<class T> inline bool isptr(const ptr<T>&) { return true; }
int
main()
{
int i;
int *pi;
auto_ptr<int> api;
ptr<int> spi;
cout << isptr(i) << ", "
isptr(pi) << ", "
isptr(api) << ", "
isptr(spi) << endl;
return 0;
}
--end cut
and have it emit "false, true, true, true" -- but, no matter how
desirable, I really don't see a realistic chance of this happening:-(.
Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia Fax: +39 (51) 597120
[ 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:
<martelli.813831475@cadlab>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: martelli@cadlab.cadlab.it (Alex Martelli) Newsgroups: comp.std.c++ Subject: Re: Determining between classes and built-in t Date: 16 Oct 1995 15:04:08 GMT Organization: Cad.Lab Spa, Bologna, Italia Sender: nntpnews@cadlab.it (NNTP Posting Services) Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <martelli.813831475@cadlab> References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Newsreader: NN version 6.5.0 #11 Content-Length: 3204 X-Lines: 81 Originator: clamage@taumet