Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11085
| From | Ruslan Mullakhmetov <tiabaldu@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: calculate number of template instantiations |
| Date | 2011-10-25 20:05 +0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <j86mo7$vgr$1@speranza.aioe.org> (permalink) |
| References | <j86c4d$vmq$2@speranza.aioe.org> <j86dpp$jop$1@dont-email.me> <2ffb8a32-5fbc-475a-8f1d-b378c05d0e93@m19g2000vbm.googlegroups.com> |
On 2011-10-25 19:30:24 +0400, Noah Roberts said:
> 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/4383602/how-to-force-inclusion-of-unused-object-definitions-in-a-library
>
Yes, I got it. I temporaly solved it by streaming value to cout. Thank
you to. now i use useless class.
the code:
size_t counter( size_t size, bool count );
//extern size_t g_hack;
template<typename T>
class Templ
{
class UseMe
{
public:
UseMe( size_t sz )
{
}
};
T _val;
UseMe _useme;
public:
Templ<T>(): _useme(size_counter)
{
}
public:
static size_t size_counter;
};
template<typename T>
size_t Templ<T>::size_counter = counter(sizeof(T), true);
#endif
--
BR, Ruslan Mullakhmetov
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
calculate number of template instantiations Ruslan Mullakhmetov <tiabaldu@gmail.com> - 2011-10-25 17:04 +0400
Re: calculate number of template instantiations Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-25 09:32 -0400
Re: calculate number of template instantiations Noah Roberts <roberts.noah@gmail.com> - 2011-10-25 08:30 -0700
Re: calculate number of template instantiations Ruslan Mullakhmetov <tiabaldu@gmail.com> - 2011-10-25 20:05 +0400
Re: calculate number of template instantiations Arne Mertz <arnemertz@googlemail.com> - 2011-10-26 02:10 -0700
Re: calculate number of template instantiations Ruslan Mullakhmetov <tiabaldu@gmail.com> - 2011-10-25 19:42 +0400
Re: calculate number of template instantiations Noah Roberts <roberts.noah@gmail.com> - 2011-10-25 09:43 -0700
Re: calculate number of template instantiations Ruslan Mullakhmetov <tiabaldu@gmail.com> - 2011-10-25 20:46 +0400
csiph-web