On Oct 25, 6:32*am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 10/25/2011 9:04 AM, Ruslan Mullakhmetov wrote:
>
> > I got an interesting problem to play with templates. It's not abstract
> > and come from real life. I'm not sure wether it has solution within C++,
> > so I need advise and you opinions.
>
> > In brief: I need to calculate number of template instantiation,
> > reasoning below.
>
> > What I need. Assume I have some template Templ<T>. In the code I
> > instantiated it several times like Templ<A>, Templ<B>, ... Next I need
> > to get number of instantiation _prior_ to creating any of instances of
> > instantiated types. The code, I believe, clarify. Note, it's not compiling.
> > [...]
>
> A static data member in a template would be instantiated once per every
> template instantiation and initialized once. *If that data member is of,
> say, 'int' type, you can initialize it by calling some function in which
> the counter would sit. *Pass an argument to that function to indicate
> whether to increment the counter before returning it or just return what
> it has.
This is close except that template members that are never used do not
make it into existence. Thus unless you actually use that static
member somewhere, it never gets initialized and thus never increases
the count. Since the goal here is to make a count variable, not an ID
or whatever, I imagine this problem is going to come up.
One way to solve this problem is to use the static variable to
initialize a non-static variable within the constructor(s) of the
object. I don't believe you actually have to use the instance member
beyond its use in the constructor, but I actually had a use for it as
an ID.
See this SO entry:
http://stackoverflow.com/questions/4...s-in-a-library