Why class membr functions ae not created only when they are needed?

I

Ioannis Vranos

AFAIK most (all?) compilers create instances of all member functions of a class even if some of them are not
used, an instances of all regular functions even if some of them are not used.

At the same time, in the case of template functions, only instances that are used are created.


Why a compiler can not create instances *only* of the regular functions, and of the member functions of a
class that are used?



Thanks.


--
Ioannis A. Vranos

C95 / C++03 Developer

http://www.cpp-software.net
 
A

Anand Hariharan

AFAIK most (all?) compilers create instances of all member functions of a class even if some of them are not
used, an instances of all regular functions even if some of them are not used.

At the same time, in the case of template functions, only instances that are used are created.

Why a compiler can not create instances *only* of the regular functions, and of the member functions of a
class that are used?

Thanks.

Note that what you note about class member functions is also true
about non-member free-standing functions as well as static class
member functions.

Take a look at this code:

<code>
#define FOO Ioannis
#define BAR Vranos

int main()
{
}
</code>

Fundamentally, your question is not too far removed from a similar
question that asks "I put this code through the pre-processor, and I
don't find my name in its output! Why not?".

A template function is not a regular function by any means. I could
say a template is a glorified macro with type checking and none of the
side-effects that macros have (but I will get lynched by compiler
writers for saying that!).

Finally, depending upon what your definition of "use" is, you can have
template functions instantiated even if they are not used e.g.,

if (false)
func<int>();

- Anand
 
J

Juha Nieminen

Anand said:
Finally, depending upon what your definition of "use" is, you can have
template functions instantiated even if they are not used e.g.,

if (false)
func<int>();

In fact, it's possible to instantiate *all* the member functions of a
template class with one single command. (Damned if I remember what it was.)
 
J

James Kanze

AFAIK most (all?) compilers create instances of all member
functions of a class even if some of them are not used, an
instances of all regular functions even if some of them are
not used.
At the same time, in the case of template functions, only
instances that are used are created.
Why a compiler can not create instances *only* of the regular
functions, and of the member functions of a class that are
used?

The compiler doesn't create instances of non template functions;
the programmer does. The compiler just compiles the code it's
given. In the case of function template, the compiler creates
functions from the template on an as needed basis, and compiles
them. And if a function is not actually used, there's nothing
to prevent a compiler from not generating any code for it.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top