🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Determining between classes and built-in t

#2230
From: Alex Martelli
Date: Tue, 17 Oct 1995 20:21
77 lines
3724 bytes
vandevod@cs.rpi.edu (David Vandevoorde) writes:
	...
>AM> So, ask for _that_... have a "template<class T> bool
>AM> can_memmove()" that returns false by default, specialize it to
>AM> return true for types that CAN be moved by memmove() (all
	...
>Or make an array_move<T>(...) function with suitable specializations.

Yup, sounds quite possibly better than my off-the-cuff suggestion;
though it would make things a bit harder on the author of some new
memmove-able class (they'd have to cut-and-paste reuse some
memmove-based array_move<T> rather than just specializing the
can_memmove thingy to return true), it also opens up possibilities
for some classes that couldn't quite be handled by memmove but
might not need the full generality of placement/new loops.  The
two ideas can be combined (just a sketch, ignoring details such as
taking care of overlaps etc...):

template <class T> inline bool can_memmove(const T&) { return false; }
inline bool can_memmove(const int&) { return true; } // etc
template <class T> void array_move(T*oldar,T*newar,int numel) {
    if(can_memmove(oldar[0])) {
	memmove((void*)newar,(void*)oldar,numel*sizeof(T));
    } else {
	for(int i=0;i<numel;i++) {
	    new((void*)&newar[i]) T(oldar[i]);
	}
    }
}
void array_move(MyClass*oldar,MyClass*newar,int numel) {
    // whatever funky implementation ... :-)
}
// and a similar array_fill for default-constructing new elements
//     (perhaps a memset() to 0 for built-in types)
// and a similar array_junk for destroying elements not moved
//     (perhaps a noop for types not needing destruction)

template <class T> class ResizeableArray {
private: T* _ar; int _ne;
public: void resize(int ne) {
	    T* ar = (T*) new char[ne*sizeof(T)];
	    int overlap = min(ne,_ne);
	    array_move(_ar,ar,overlap);
	    if(ne>overlap) array_fill(ar,overlap,ne);
	    if(_ne>overlap) array_junk(_ar,overlap,_ne);
	    delete[] (char*)_ar;
	    _ar=ar; _ne=ne;
	}
        // etc...
};


>AM> THAT would be a neat little thing indeed to have -- a rule
>AM> allowing a template to co-exist for both "any class" AND "any
>AM> pointer", with the latter taking precedence if both versions exist
>AM> and the template is instantiated for a pointer...

>Such a rule is in the DWP (let me know if your compiler supports it,
>mine doesn't :-). BTW, this is called partial specialization...

Ah -- wonderful!  I had missed this novelty... chapter and verse of
the DWP please...?  No, g++ 2.7.0, the compiler I use, does not
support it yet (I did try as soon as I read this...:-).


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.813918684@cadlab>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak> <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU> <martelli.813831475@cadlab> <VANDEVOD.95Oct16193053@avs.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: martelli@cadlab.cadlab.it (Alex Martelli)
Newsgroups: comp.std.c++
Subject: Re: Determining between classes and built-in t
Date: 17 Oct 1995 20:21:09 GMT
Organization: Cad.Lab Spa, Bologna, Italia
Approved: matt@physics10.Berkeley.EDU (Matt Austern)
Message-ID: <martelli.813918684@cadlab>
References: <Pine.SGI.3.91.951003143924.20596A-100000@gusak>  <450v2o$f3g@engnews2.Eng.Sun.COM> <45mor5$fms@jupiter.SJSU.EDU>  <martelli.813831475@cadlab> <VANDEVOD.95Oct16193053@avs.cs.rpi.edu>
NNTP-Posting-Host: physics10.berkeley.edu