Topic: virtual template methods ?


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/01/18
Raw View
In article <36A0CD1B.DA8C0DEA@lovi.com>, Jacques Lovi <jacques@lovi.com>
writes
>But I'd like to know if someone sees something against the following:
>
>typedef enum {e1,e2} E;
>
>class C
>{
>    template<class E> virtual void f3();
>};
>
>I'm aware  that the current C++ standard doesn't support this but I
>think tthat it could.

Only if you start providing significance to the <class E>

I think you intended something more like:

template<E e> virtual void f3();

>
>Do anyone see why it couldn't ?
But the problem with even the rewritten code is that there are many
available values other than those explicitly named (and there must be to
support current code)

The two mechanisms (virtual and template) are intended for quite
distinct tasks and ones that I do not think merge.  Virtual is for
distinct implementations for variants of a type while templates are for
common implementations for unrelated types.
>
>Jacques L   vi.
>
>PS: Please answer to jacques@lovi.com too.

Learn to read things where you post them. Sometimes we go offline
because a topic needs detailed discussion on a one-one basis.


Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jshiva@bigfoot.com
Date: 1999/01/18
Raw View
In article <36A0CD1B.DA8C0DEA@lovi.com>,
  jacques@lovi.com wrote:
 > Hi everybody,
 >
 > I understand why there can't be something like that:
 >
 > class A
 > {
 >     template<class T> virtual void f1();
 > };
 >
 > Since the compiler won't know how many entries should be put into the
 > virtual table of objects of class A.
 >
 > That's the same thing with:
 >
 > class A
 > {
 >     template<class int> virtual void f2();
 > };

this won't compile because class int is illegal.
maybe you meant
class A
{
   template <int i> virtual void f2();
};
 >
 > But I'd like to know if someone sees something against the following:
 >
 > typedef enum {e1,e2} E;
 >
 > class C
 > {
 >     template<class E> virtual void f3();
 > };

Same thing here.
Maybe you meant.
class C
{
  template<E e> virtual void f3();
};


 >
 > I'm aware  that the current C++ standard doesn't support this but I
 > think tthat it could.
 >
 > Do anyone see why it couldn't ?
 >
 > Jacques Lvvi.

cheers,
Shiva
comp.lang.c++ FAQ :http://www.cerfnet.com/~mpcline/c++-faq-lite/
http://members.xoom.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Larry Brasfield" <clcppm-poster@this.is.invalid>
Date: 1999/01/18
Raw View
Jacques Lovi wrote in message <36A0CD1B.DA8C0DEA@lovi.com>...
>Hi everybody,
Hi guy.

You probably should have trimmed the newsgroup
list to any one of the three that got this.

>I understand why there can't be something like that:
>
>class A
>{
>    template<class T> virtual void f1();
>};
>
>Since the compiler won't know how many entries should be put into the
>virtual table of objects of class A.

Sounds good.

>That's the same thing with:
>
>class A
>{
>    template<class int> virtual void f2();

Above line makes no sense to me or the
compiler I often use.  Perhaps you meant:
     template <int N> virtual void f2();

>};
>
>But I'd like to know if someone sees something against the following:
>
>typedef enum {e1,e2} E;
>
>class C
>{
>    template<class E> virtual void f3();

Same problem as above.  You might mean:
    template<E an_E_value> virtual void f3();
As you wrote it, 'E' is a formal template parameter
name that has no relation (except similar spelling)
to the 'E' that names the preceeding enum type.

>};
>
>I'm aware  that the current C++ standard doesn't support this but I
>think tthat it could.

Perhaps it would be theoretically possible to
reserve as many v-table entries as there are
enum E enumeration values.

>Do anyone see why it couldn't ?

Given that virtual member functions are not
allowed, why complicate the rule for this
special case?  I really don't see any value
of it that would not be better achieved in
other ways.  I agree that it could be done,
but "could be" does not mean "should be".

If it were done, the effect would be to have
a lot of instantiations doing what a single
member function could do if you just pass
in the enum value.  I don't see the point.

>PS: Please answer to jacques@lovi.com too.

Post here, read here, please.

--Larry Brasfield
Above opinions may be mine alone.
(Humans may reply at unundered larry_br@sea_net.com )





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/01/19
Raw View
On 18 Jan 1999 10:56:17 -0500, Francis Glassborow
>In article <36A0CD1B.DA8C0DEA@lovi.com>, Jacques Lovi <jacques@lovi.com>

>>But I'd like to know if someone sees something against the following:

>>typedef enum {e1,e2} E; //LINE1
>>
>>class C
>>{
>>    template<class E> virtual void f3(); // LINE5
>>};
>>
>>I'm aware  that the current C++ standard doesn't support this but I
>>think tthat it could.

BTW, the 'E' in the LINE5 hides the 'E' in LINE1.  Ie, it's just
as if you wrote either of these:
      template<class F> virtual void f3(); // LINE5
      template<class  > virtual void f3(); // LINE5
Anyway, enums are integral constants, so they are allowed as
non-type template arguments, as in this:

>template<E e> virtual void f3();



>>Do anyone see why it couldn't ?

The technical reason why it can't be done is that the highly
efficient virtual table mechanism implementors normally use
wouldn't be applicable here.  Here's the idea of this
implementation.  Say your class C looks like this:
 class C
 {
    virtual ~C();
    virtual void f(int);
    virtual void f(double);
    virtual void g();
 };

Now the virtual table of C has 4 functions in it.
 vtable<C>[0] points to C::~C()
 vtable<C>[1] points to C::f(int)
 vtable<C>[2] points to C::f(double)
 vtable<C>[3] points to C::g()

Objects derived from C inherit this virtual table layout.  So
 class D : public C
 {
    virtual void h();
 };

Now the virtual table of D has 5 functions in it.
 vtable<D>[0] points to D::~D()
 vtable<D>[1] points to D::f(int)
 vtable<D>[2] points to D::f(double)
 vtable<D>[3] points to D::g()
 vtable<D>[4] points to D::h()

So calling a function "c->g()" essentially says
 [X] get the virtual pointer of 'c'
 [X] deference it to get to the virtual table
 [X] find the address of vtable<>[3]
 [X] call this function


Now if virtual functions were allowed to be templates, then the
virtual table would be infinite.  Eg,
 class C
 {
    virtual ~C();
    template <class> virtual void f();
    virtual void g();
 };

Here's what the virtual table might look like:
 vtable<C>[0] points to C::~C()
 vtable<C>[1] points to C::f<int>()
 vtable<C>[2] points to C::f<double>()
 vtable<C>[2] points to C::f<short>()
 ...
 vtable<C>[infinity+2] points to C::g()


Surely, there is a way out.  Encode the virtual table entry as a
template.
 vtable<C>[0] points to C::~C()
 vtable<C>[1] points to template <class T> C::f<T>()
 vtable<C>[2] points to C::g()

This complicates the process of calling a virtual function.
Everytime we get the address of a function, we have to resolve
whether it is a template or non-template function.  This
imposes a time overhead.

Also, the linker would have to go to great lengths to ensure that
we have every template function that client may use.  Eg, if we say,
   c->template f<int>();
then the compiler has to instantiate C::f<int> and D::f<int>.
In fact, the linker will have to find every class CC derived from
C and instantiate CC::f<int>.  This complicates the link process
and may also lead to code bloat.

There is a way to do what you want, but only for a finite number
of types T.  Have the virtual function take a type_info object.
But now, derived classes will have to do a switch-case on this
type_info object.  (OK, technically, they would have to do a
series of if-else-else, or lookup in a map, lookup in a hashtable,
lookup in a vector using binary_search, etc).

class C
{
   virtual ~C();
   virtual void f(const type_info&) = 0;
};


>But the problem with even the rewritten code is that there are many
>available values other than those explicitly named (and there must be to
>support current code)

Yes, and the implementation of D::f(const type_info&) is going
to be very very painful.



>The two mechanisms (virtual and template) are intended for quite
>distinct tasks and ones that I do not think merge.  Virtual is for
>distinct implementations for variants of a type while templates are for
>common implementations for unrelated types.

OK, but it might make sense in some cases.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jacques Lovi <jacques@lovi.com>
Date: 1999/01/18
Raw View
Hi everybody,

I understand why there can't be something like that:

class A
{
    template<class T> virtual void f1();
};

Since the compiler won't know how many entries should be put into the
virtual table of objects of class A.

That's the same thing with:

class A
{
    template<class int> virtual void f2();
};

But I'd like to know if someone sees something against the following:

typedef enum {e1,e2} E;

class C
{
    template<class E> virtual void f3();
};

I'm aware  that the current C++ standard doesn't support this but I
think tthat it could.

Do anyone see why it couldn't ?

Jacques L   vi.

PS: Please answer to jacques@lovi.com too.





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]