From -4443530654460065189
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,1bc187fa7b436cfb
X-Google-Attributes: gidf78e5,public
From: "Sievert, James A" <James.Sievert@UNISYS.com>
Subject: Re: Collections of template classes??
Date: 1998/01/15
Message-ID: <376ABEF04F3ED011816C00A02461F3F999981A@exchang2.rsvl.unisys.com>#1/1
X-Deja-AN: 316414513
X-Original-Date: Thu, 15 Jan 1998 06:14:38 -0700
X-Priority: 3
Originator: austern@isolde.mti.sgi.com
Content-Type: text/plain
Organization: -
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBNL7Kf0y4NqrwXLNJAQFnzgH9EPw/a0ZVXtUsYqjI0eOtLIYN0Y1/63pC fwVsiKrP1pBCKgowPp1FnkmHc3iY9y6AwvX0xkHT5qOyU7nESwsf3Q== =6Vfx
Newsgroups: comp.std.c++


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 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



