instance counting

S

stephan beal

Good morning, C++ers,

A couple days ago i came across a useful trick for counting instances of a
given class, and i thought i'd pass it on:

Conventionally, as described in several books, to do instance counting we
subclass some base type and add a counter to that base class. IMO that is
completely unnecessary:

/**
Utility function to help keep an instance count of T.
*/
template <typename T>
unsigned long & inst_count() { static unsigned long c = 0; return c; }

Then simply do this in the ctors:
++inst_count<MyType>();

and in the dtor:
--inst_count<MyType>();

This offers a feature the conventional approach does not: it allows us to
track instance counts separately for, e.g. A and B, when B subclasses A.
That it, if we use some "pure A" objects and some B objects we can
independently track A and B counts by using inst_count<> in both A and B.

Of course, this approach does suffer from the potential problem of client
code doing:

inst_count<MyType>() = 10;

but i think that's far-fetched enough that it can be essentially ruled out.

--
----- stephan beal
http://s11.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
 
V

Victor Bazarov

stephan beal said:
Good morning, C++ers,

A couple days ago i came across a useful trick for counting instances of a
given class, and i thought i'd pass it on:

Conventionally, as described in several books, to do instance counting we
subclass some base type and add a counter to that base class. IMO that is
completely unnecessary:

/**
Utility function to help keep an instance count of T.
*/
template <typename T>
unsigned long & inst_count() { static unsigned long c = 0; return c; }

Then simply do this in the ctors:
++inst_count<MyType>();

and in the dtor:
--inst_count<MyType>();

This offers a feature the conventional approach does not: it allows us to
track instance counts separately for, e.g. A and B, when B subclasses A.
That it, if we use some "pure A" objects and some B objects we can
independently track A and B counts by using inst_count<> in both A and B.

Of course, this approach does suffer from the potential problem of client
code doing:

inst_count<MyType>() = 10;

but i think that's far-fetched enough that it can be essentially ruled
out.

Yes, this expression is far-fetched enough. However, this isn't:

void print(unsigned long& a) { // erroneously put '&' here
cout << a;
a = 0; // hidden reset of a variable -- unintended "reuse"
// of the parameter as a local variable
}

...
print(inst_count<MyType>()); // oops

But if the user remembers to put ++inst_count<>() in the c-tors,
then he will probably remember not to make a mistake like that...

Victor
 

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,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top