🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: An STL helper -- and template and type shenanigans

#3096
From: Pulkkinen Esa
Date: Tue, 23 Jan 1996 16:07
58 lines
2481 bytes
In article <30FCDA77.69CB@trilogy.com>,
D. Allan Drummond <allan.drummond@trilogy.com> wrote:
[an example and discussion about type dereferencing removed]
>I really want to write something like this (the following code is
>nonsensical but should convey the spirit of the idea):
>
>template<class Container>
>void deep_copy( Container& from, Container& to )
>{
>    to = from; // expand to make sure TO is as big as FROM.
>               // should be cheap, if this is really a list of pointers.
>    Container::const_iterator f( from.begin() );
>    Container::iterator t( to.begin );
>
>    while( (f != from.end()) && (t != to.end()) && *f )
>        *t++ = new (*Container::value_type)( **f++ );
>}
>
>It seems as though there's no REAL reason to have to split it into two
>functions, but the "type dereferencing" is crucial.

Well, you can generalize the type deferencing scheme using a partial
specialization:

template <class T> class type_dereferencer
{
};

template <class T>
class type_dereferencer<T*>
{
public:
  typedef T base_type;
};

Of course you would also have to specialize for user-defined
pointer types and STL iterators.

Now you can use
type_dereferencer<Container::value_type>::base_type in place of 
(*Container::value_type) in your example.

And if typedef templates were allowed, you could use a slightly better
syntax:

template <class T>
typedef type_dereferencer<T>::base_type type_dereference;
--
   Esa Pulkkinen                        | C++ programmers do it virtually
   E-Mail:  esap@cs.tut.fi              | everywhere with a class, resulting
   WWW   :  http://www.cs.tut.fi/~esap/ | in multiple inheritance.




[ 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: <4e2dqo$gh@peippo.cs.tut.fi>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <30FCDA77.69CB@trilogy.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: esap@cs.tut.fi (Pulkkinen Esa)
Newsgroups: comp.std.c++
Subject: Re: An STL helper -- and template and type shenanigans
Date: 23 Jan 1996 16:07:45 GMT
Organization: Tampere University of Technology
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <4e2dqo$gh@peippo.cs.tut.fi>
References: <30FCDA77.69CB@trilogy.com>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: korppi.cs.tut.fi
Nntp-Posting-User: esap
Content-Length: 1649
Originator: clamage@taumet