🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: japan.comp.lang.visual-c++
Article #5

Re: $B%3%s%9%H%i%/%?Fb$rHr$1$k$Y$-=hM}(B

#5
From: okahisa
Date: Fri, 12 May 2000 00:00
76 lines
2997 bytes
$B2,5W$G$9!#(B

  $B<+8J%U%)%m!<$r>/!9!#(B

<8f95bs$vk9$1@csgw2.cs.ehime-u.ac.jp>$B$N5-;v$K$*$$$F(B
$B;d$O=q$-$^$7$?!#(B

>>         const int N = 4;
>>         void* p = malloc(N * sizeof(CHoge));
>>         if (p) {
>>             CHoge* vips = new(p) CHoge[N];
>>             vector< hitman<CHoge> > request(vips, vips + N);


  $B$3$3$G!"(Bvector $B$N3NJ]$K<:GT$9$k$H!"(Bvips $B$,GK4~$5$l$J$$$G$9$M!#(B
$B$=$s$J$o$1$G=$@5HG!#(B

    #include <iostream>
    #include <vector>
    #include <new>
    using namespace std;
    
    template <class T> class hitman
    {
    public:
        typedef std::vector<const T*> targets;
    
        hitman(const T* p, const size_t N) : t()
        // $BK\Ev$O!"(B':' $B$NA0$K(B try $B$H=q$-$?$$$H$3$m$@$,!"(Bgcc 2.95.2
        // $B$G$O$^$H$b$KF0$+$:!"(BBorland C++ 5.5 $B$G$O%(%i!<$K$J$k!#(B
        {
            try { t.resize(N); }
            catch (...) {
                for (size_t i = 0; i < N; i++) p[i].~T();
                throw;
            }
            for (size_t i = 0; i < N; i++) t[i] = p + i;
        }
    
        ~hitman() { for (size_t i = 0; i < t.size(); i++) t[i]->~T(); }
    private:
        targets t;
    };
    
    class CHoge
    {
    public:
        CHoge() { cerr << "CHoge: Ogyaaa!!" << endl; }
        ~CHoge() { cerr << "CHoge: Ugyaaa!!" << endl; }
    };
    
    // $B=hM}7O$K$h$C$F$OI,MW$+$b$7$l$J$$!#(B
    // void* operator new[](const size_t, void* p) throw() { return p; }
    
    int main()
    {
        try {
            const size_t N = 4;
            void* p = malloc(N * sizeof(CHoge));
            if (p) {
                CHoge* vips = new(p) CHoge[N];
                hitman<CHoge> request(vips, N);
                // ...
            }
            return 0;
        }
        catch (...) { exit(1); }
    }

  $B>/$7(B placement new $B$NMxE@$K$D$$$F@bL@$9$k$H!"(Bplacement new
$B$rMQ$$$F<+J,$G3NJ]$7$?NN0h$KG[Ns$rDj5A$9$k$h$&$K$9$l$P!":G8e(B
$B$NMWAG$G9=C[$K<:GT$7$F$b!"$=$l$^$G$K3NJ]$5$l$F$$$?MWAG$O<+F0(B
$BE*$KGK4~$5$l$^$9!#(B

--
Email: okahisa@cs.ehime-u.ac.jp
Technical Details
Message-ID: <8ffup6$qjt$1@csgw2.cs.ehime-u.ac.jp>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8evptd$4m2$2@newsgw8.odn.ne.jp> <8f0qal$jrh$1@nwms2.odn.ne.jp> <8f5cue$rjs$1@newsgw8.odn.ne.jp> <8f95bs$vk9$1@csgw2.cs.ehime-u.ac.jp>

Show Full Headers
Complete Article Headers
X-Google-Language: Japanese,JIS
X-Google-Thread: f71b3,d8108d20cf4aa80b
X-Google-Attributes: gidf71b3,public
X-Google-Thread: 10a2c7,d8108d20cf4aa80b
X-Google-Attributes: gid10a2c7,public
From: okahisa@cs.ehime-u.ac.jp
Subject: Re: $B%3%s%9%H%i%/%?Fb$rHr$1$k$Y$-=hM}(B
Date: 2000/05/12
Message-ID: <8ffup6$qjt$1@csgw2.cs.ehime-u.ac.jp>
X-Deja-AN: 622408894
References: <8evptd$4m2$2@newsgw8.odn.ne.jp> <8f0qal$jrh$1@nwms2.odn.ne.jp> <8f5cue$rjs$1@newsgw8.odn.ne.jp> <8f95bs$vk9$1@csgw2.cs.ehime-u.ac.jp>
X-Complaints-To: usenet@cs.ehime-u.ac.jp
X-Trace: csgw2.cs.ehime-u.ac.jp 958103142 27261 133.71.90.66 (12 May 2000 03:45:42 GMT)
Organization: Dept.of Computer Science,Ehime University,Japan
NNTP-Posting-Date: 12 May 2000 03:45:42 GMT
Newsgroups: japan.comp.lang.visual-c++,fj.comp.lang.c++