From -4200344803941297607
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,1bc187fa7b436cfb
X-Google-Attributes: gidf78e5,public
From: "Jim Sievert" <jas1@rsvl.unisys.com>
Subject: Re: Collections of template classes??
Date: 1998/01/12
Message-ID: <01bd1f88$014d5320$27e93dc0@jas1>#1/1
X-Deja-AN: 316366180
X-NNTP-Posting-Host: jas1.rsvl.unisys.com
References: <01bd1d09$c8622bb0$27e93dc0@jas1> <fjh-980113-023744@cs.mu.oz.au>
X-UID: 0000000001
X-Status: $$$$
Organization: Unisys - Roseville, MN
Newsgroups: comp.std.c++
Originator: clamage@taumet


Fergus Henderson <fjh@cs.mu.oz.au> wrote in article
<fjh-980113-023744@cs.mu.oz.au>...
> Jim Sievert <jas1@rsvl.unisys.com> wrote:
> 
> Why?

I've been doing some work with STL lately.  From what I've read, STL is
touted for its ability to generalize type-related expressions and
algorithms.  In much of STL, this generalization is done with no virtual
functions.  Compilers are then able to reduce these expressions and
algorithms into very tight code.  When possible, we've used STL-like
techniques for solving various problems.  The resulting code is highly
optimized.

I'd like to apply the same ideas of generic programming across collections
of different object types.

> 
> You're going to need run-time dispatch, so virtual functions seem
> like the obvious thing to use.
> 

I'm not so sure...

> >Consider the following class:
> >
> >template< class T >
> >class CFoo
> >{
> >public:
> >   CFoo( T p ) : m_p( p ) {}
> >
> >   template< class C >
> >      bool operator ==( C to ) { return to == m_p; }
> >
> >private:
> >   T m_p;
> >};
> >
> 
> You lost me here.  If the single vector contains both CFoo<int> and
> CFoo<CBar>, then there are at least two operator ==() functions involved.
> I don't see how a compiler could figure out at compile time
> which one to use when iterating over a vector whose elements
> could be of either type.

Let me change the example slightly:

template< class T >
class CFoo
{
public:
   CFoo( T p ) : m_p( p ) {}

    bool operator ==( CBarBar to ) { return to == m_p; }

private:
   T m_p;
};

For all instances of the CFoo template, operator ==() is at a fixed offset.
 For example, with CFoo< int > and CFoo< CBar >, operator ==() is always at
offset 0.  Therefore, iterating over a vector with instances of CFoo< int >
and CFoo< CBar > would imply invoking offset 0 with a given CBarBar
parameter.

The original example code forces the compiler to create a fixed set of
offsets for all CFoo template instances where only some of those offsets
are valid for a particular instance of CFoo.  This may get messy?



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]



