Why class member functions are not created only when they are needed?

J

James Kanze

So let's assume we have a concrete type (stand alone class),
e.g. a complex class. And we don't use all its member
functions in a program, why should we have them imposed to us?

If it's in the application, there's no reason to write any
function which isn't used. If it's in a library, which is used
by several applications, you put each function in a separate
source file, and each application only contains the code it
uses.

You seem to be confusing instantiation and presence in the
client program. Instantiating a template means expanding it for
a specific set of template arguments, converting the function
template into a function. Instantiation has no meaning for non
template functions, since they are already "functions". Whether
a function (resulting from a template instantiation or not) is
incorporated into the application is a completely different
question (except that function templates which have not been
instantiated obviously cannot be incorporated into the
application, since there is no function to incorporate). How
you specify which functions are incorporated into the final
application is implementation defined: if the function is in a
library, it will only be incorporated if the application uses
something in the object file it resides in (and I think in some
cases, only if the function is actually used). Normally, when
writing a library, you put each function in a separate source
file, which means that each function ends up in a separate
object file, which means that a function will only end up in the
application if it is actually used.
 
A

Alf P. Steinbach

* James Kanze:
If it's in the application, there's no reason to write any
function which isn't used. If it's in a library, which is used
by several applications, you put each function in a separate
source file, and each application only contains the code it
uses.

You seem to be confusing instantiation and presence in the
client program. Instantiating a template means expanding it for
a specific set of template arguments, converting the function
template into a function. Instantiation has no meaning for non
template functions, since they are already "functions". Whether
a function (resulting from a template instantiation or not) is
incorporated into the application is a completely different
question (except that function templates which have not been
instantiated obviously cannot be incorporated into the
application, since there is no function to incorporate). How
you specify which functions are incorporated into the final
application is implementation defined: if the function is in a
library, it will only be incorporated if the application uses
something in the object file it resides in (and I think in some
cases, only if the function is actually used). Normally, when
writing a library, you put each function in a separate source
file, which means that each function ends up in a separate
object file, which means that a function will only end up in the
application if it is actually used.

He he. Don't you think the cost of having one routine per file is a little too
much for reducing executable size with a dumb linker? How about e.g. (1) in the
case of a haphazard collection of routines, collect related stuff together,
and/or (2) simply accepting the size (and how much does that technique /really/
save in size?), or (3) switching to a better compiler and linker?

AFAIK very few places, if any, except yours, have 1 routine per file; it sounds
like an inordinate overhead in programming and administration time.

But I admit the possibility that this is perhaps a technique that's become
popular the last few years, for some libraries I've never seen?


- Alf
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top