This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: empty function optimizations



> Factoring might be a fair solution for the Linux folks' empty functions.
> That was Geert's original proposal.  Unfortunately the source of my problem
> is mostly C++ templates.  I don't have control to factor out the common
> code.  Well, sure I could have control but then that is like saying "don't
> use templates".

No, this is not true.  The technique you need to use is called partial
specialization.  A typical use is, say, if you have Container<T>, but
you can use the same code for any case where T is a pointer, or T is
a PODS (plain old data structure).

See Chapter 13.5 of Stroustrup's "The C++ Programming Language, 3rd
Edition" for a good tutorial on how to use this technique.

Ideally, the STL we provide should use a lot more partial specialization;
for example

template <class _Tp, class _Alloc = allocator<_Tp> >
class vector<*_Tp> : private vector<void*, _Alloc>
{
	...
};

(that is, define vector, for any pointer, in terms of vector<void*>
and use all-inline methods).

The SGI STL already provides some hooks for partial specialization,
in type_traits.h, but these are currently underused.

> I understand this could break conforming programs, but honestly I have to
> say comparing function pointers for equality is in pretty bad taste
> (entirely IMHO).

That's the least of the problems.  You seem to want the compiler or linker
to figure out for you that, say, vector<unsigned> and vector<float> can
share the same code for the copy constructor, because on your platform,
both are four bytes and it's just data movement.  But what if your
platform has different instructions to copy ints than to copy floats?  The
code will be different, even though the same code could in principle be
used.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]