STL & reducing code bloat

S

Salvador I. Ducros

Greetings all,

I was hoping someone might be able to point me in the right direction. I'm
currently
using std::vector to maintain several lists whose objects are of unrelated
types (classes).
I've seen mentioned here and there a technique that uses 'void *' and
something called
'template specialization' that would reduce the amount of code generated for
the
std::vector's.

I don't fully understand how I might use std::vector<void *> without having
to constantly
cast object pointers to and from 'void *'. Might someone point me to a
resource that
would explain this technique in greater detail?

Regards,

Salvador Ducros
(e-mail address removed)
 
A

Andrew Koenig

Salvador> I was hoping someone might be able to point me in the right
Salvador> direction. I'm currently using std::vector to maintain
Salvador> several lists whose objects are of unrelated types
Salvador> (classes). I've seen mentioned here and there a technique
Salvador> that uses 'void *' and something called 'template
Salvador> specialization' that would reduce the amount of code
Salvador> generated for the std::vector's.

I guess the first question to ask is this: Is the amount of code
in question a serious problem?
 
E

Evan

Salvador I. Ducros said:
Greetings all,

I was hoping someone might be able to point me in the right direction. I'm
currently
using std::vector to maintain several lists whose objects are of unrelated
types (classes).
I've seen mentioned here and there a technique that uses 'void *' and
something called
'template specialization' that would reduce the amount of code generated for
the
std::vector's.

I don't fully understand how I might use std::vector<void *> without having
to constantly
cast object pointers to and from 'void *'. Might someone point me to a
resource that
would explain this technique in greater detail?

Regards,

Salvador Ducros
(e-mail address removed)

If you are storing a known set of types, you might check out Boost's
tuple class: http://boost.org/libs/tuple/doc/tuple_users_guide.html
 
E

Evan

Doh... I posted another post, but forget to finish it first... (and I
can't reply to it directly as I'm using Google and it takes forever to
update)

Anyway, about Boost::tuple, I'm not sure if that'll help. It probably
won't reduce code bloat anyway... But it may, so that's why I
suggested it.
 
S

Salvador I. Ducros

Evan said:
Doh... I posted another post, but forget to finish it first... (and I
can't reply to it directly as I'm using Google and it takes forever to
update)

Anyway, about Boost::tuple, I'm not sure if that'll help. It probably
won't reduce code bloat anyway... But it may, so that's why I
suggested it.

I'll check it out. It sounds like it might be useful for something else I've
been working on. Thanks,

Salvador I. Ducros
(e-mail address removed)
 
H

Howard Hinnant

| | > Salvador> I was hoping someone might be able to point me in the right
| > Salvador> direction. I'm currently using std::vector to maintain
| > Salvador> several lists whose objects are of unrelated types
| > Salvador> (classes). I've seen mentioned here and there a technique
| > Salvador> that uses 'void *' and something called 'template
| > Salvador> specialization' that would reduce the amount of code
| > Salvador> generated for the std::vector's.
| >
| > I guess the first question to ask is this: Is the amount of code
| > in question a serious problem?
| >
| > --
| > Andrew Koenig, (e-mail address removed)
|
| No. Not a problem. I simply wanted to educate myself about ways to avoid
| code bloat when using STL (& templates in general) for if and when it ever
| becomes a problem.
|
| On the other hand, I do find the amount of code being generated a bit much.
| I was able to reduce the code size significantly by using a generic
| container
| implemented in plain C. I'd rather use STL which is why I started searching
| for ways of reducing code bloat when using STL (std::vector in this specific
| case).

Bjarne Stroustrup's excellent text: The C++ Programming Language"
devotes a small section to this technique. The Metrowerks
implementation goes beyond the "void* optimization" and implements this
code size optimization across all pods (e.g. unsigned short and short
also share the same code).
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top