Can a template method be pure virtual?

C

Christopher Pisz

I want to require that every subclass override the method.

template<typename T>
virtual Exception & operator << (const T & value) = 0;

such that the subclass type is guarentee to be returned when the method
is called rather than returning the base class by accident, because
someone forgot to override the method.
 
J

Jorgen Grahn

I want to require that every subclass override the method.
template<typename T>
virtual Exception & operator << (const T & value) = 0;

such that the subclass type is guarentee to be returned when the method is
called rather than returning the base class by accident, because someone
forgot to override the method.

Since T can be any type or a class, what that literally says is that any
subclass, of a class that has such a method, has to override this operator
for each and every class that can be used in C++.

Since this is an infinite set of classes, there's simply not enough atoms in
this universe in order to be able to implement such a class. Sorry.[/QUOTE]

I don't buy that particular argument. A template is instantiated for
a finite set of T. If you're not building a library, you even have
control over these Ts.

/Jorgen
 
N

Nobody

I don't buy that particular argument. A template is instantiated for
a finite set of T. If you're not building a library, you even have
control over these Ts.

It would be incompatible with separate compilation. The vtable layout
(which has to be the same for all translation units) would depend upon
the set of classes for which the template is instantiated.

The problem isn't that the set is infinite, but that it's open-ended when
it needs to be fixed.
 
Ö

Öö Tiib

It would be incompatible with separate compilation. The vtable layout
(which has to be the same for all translation units) would depend upon
the set of classes for which the template is instantiated.

The problem isn't that the set is infinite, but that it's open-ended when
it needs to be fixed.

Linker has possibly to generate some more code and positions of different
instantiations in vtable have to be decided link-time. That does not sound
too hard (unless when trying to do it with dll) so someone could offer it as
extension.

What I dislike are the potential linker error messages about those perfectly
forwarding virtuals called somewhere deep in library not compiling properly.
Templated virtual is harder to use than to implement.
 
8

88888 Dihedral

Jorgen Grahnæ–¼ 2013å¹´6月14日星期五UTC+8下åˆ4時28分44秒寫é“:
On Fri, 2013-06-14, Sam wrote:

...







I don't buy that particular argument. A template is instantiated for

a finite set of T. If you're not building a library, you even have

control over these Ts.



/Jorgen



--

// Jorgen Grahn <grahn@ Oo o. . .

\X/ snipabacken.se> O o .

C++ is a language deisgned to generate some compiled machine code/
libs in mltiple passes of the compilation process.

C++ is still not dynamical typed with the auto GC and a hiden
interpretrs in executables as those in the 5th and 6th gen languages such as python, jython, ruby, erlanng, pike and etc..

But objetive C is qualified to be in the list of 5 -th gen
language.
 
Ö

Öö Tiib

Wouldn't work with dynamically loadable libraries, where the library is
fixed.

Yes, I meant that with "unless with dll". The support to dynamic linkage is
extension as well. There are no modules yet in C++. Virtual templates could
be extension that can not be used together with that other extension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top