calculate number of template instantiations

  • Thread starter Ruslan Mullakhmetov
  • Start date
R

Ruslan Mullakhmetov

Hi folks,

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.

#include <iostream>

template<typename T>
struct Templ
{
T some_dummy_value;
};

class A{};
class B{};

struct InstanceA
{
Templ<A> inst;
void foo(){}
};

struct InstanceB
{
Templ<B> inst;
void foo(){}
};


//here the question
struct Counter
{
// ???????????
enum
{
INSTANCES_NUM = // ????????
};
}

int main()
{
// I need this value _before_ creating any objects of
// instantiated types.
std::cout << Counter::INSTANCES_NUM << std::endl;

// And only now (!) create instances of InstanceA, InstanceB
InstanceA a; a.foo();
InstanceB b; b.foo();
}

As you see I need number of instantiation before creating any objects
of instantiated types, so it's almost (at least i see so) the same as
knowing at compile time, so i wrote

enum { INSTANCE_NUM };


The reasoning. What I wrote above is extraction of what I need for
simpleness and clearness. The real problem.
I recently wrote a wrapper for plain C written simulation system (SS)
designed with performance issues in mind, E.G. data locality. So i
chose to use template-based approach for wrapping. This discrete
event-driven SS managed memory itself and asks me for maximum size of
messages _IT_ will create for me. So i wrote wrapers, but now I need to
feed it with maximum message size of templates (==wraper for messages)
i created.

The flow.
---------------

#include <ss.h>

struct MsgA{};
struct MsgB{};

void fill_a( MsgA * msg ){}
void fill_b( MsgB * msg ){}

// callback for SS
void event_handler( void * msg ){}

// callback for SS
void init()
{
void * event = ss_create_event(time, recepient);
MsgA * msg = (MsgA*) ss_message_data(event);
fill_a( msg );
ss_send_msg( event );

// the same with msg B
...
};

SS_AgentType agent_type = {
&init,
&event_handler
};

int main()
{
agents_num = 10;
max_msg_size = max( sizeof( MsgA ) , sizeof( MsgB ) );

ss_init( agents_num, max_msg_size, agent_type );
ss_run();
// process results.
ss_stop();
}
----------------

I wrote wrapper that (almost) statically dispatch agents and message
types and i avoided cast's from void *. But I still need to calculate
message sizes. I want to avoid it to. It's quite the same as calculate
number of instances.

BR, Ruslan Mullakhmetov
 
V

Victor Bazarov

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.

It's not a problem, it sounds very much like a homework.

V
 
N

Noah Roberts

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/...ion-of-unused-object-definitions-in-a-library
 
R

Ruslan Mullakhmetov

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.

It's not a problem, it sounds very much like a homework.

V

Thanks a lot. I moved in wrong direction trying to get this info at
compile time and stragling with fact that templates are single assigned.

There is one problem that compliler "optimize" my code and wipe out
static member if i do not use it, say, print to stdout. i use clang. is
it permitted by the standard? because corresponding initializing
function is not called to! As far as i understand, c++ assume that
function do not have a states, so it is normal to optimize in such way?

Does it dependen on number of translation units (cpp files)? I checked
and it seems to be working. E.G. I instantiated in different cpp files
and it shows overall information from both files. ask just in case.

the code.

// templ.h
#ifndef _TEMPL_H
#define _TEMPL_H

#include <iostream>
#include <cstdlib>

size_t counter( size_t size, bool count );

template<typename T>
class Templ
{
T _val;
public:
void foo()
{
//std::cout << "foo: " << _val.val << std::endl;
}

Templ<T>(char v)
{
std::cout << "Templ init " << size_counter << std::endl;
_val.val = v;
}
public:
static size_t size_counter;
};

template<typename T>
size_t Templ<T>::size_counter = counter(sizeof(T), true);

#endif

// templ.c
#include <algorithm>
#include "templ.h"

size_t counter( size_t size, bool count )
{
static size_t max_size = 0;

//printf( "counter %lu\n", max_size );
if( count )
{
max_size = std::max( max_size, size );
}

return max_size;
}
 
R

Ruslan Mullakhmetov

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/...ion-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
 
A

Arne Mertz

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.

Jut to nit-pick a bit here, this approach only counts instantiated
and
_at_least_once_constructed_ templates, because if you instantiate the
class template but never use its constructor, you get the same Problem
of the never-used-thus-never-instantiated template member. Example:

int instanceCount(bool increase = false) {
static int ic = 0;
if (increase) ++ic;
return ic;
}

template <class I>
struct instCnt {
static int unused;
instCnt() { (void)unused; }
};

template <class I>
int instCnt<I>::unused = instanceCount(true);

#include <iostream>
using std::cout;
int main() {
instCnt<int> ici;
cout << sizeof(ici) << ','
<< sizeof(instCnt<double>) << ','
<< instanceCount() << '\n';
}

outputs 1,1,1 on gcc, because instCnt is only once constructed,
albeit twice instantiated.

If one wants to count all instantiations, not only the ones
actually constructed, it gets hard. I tried it by taking
the address of the static variable an passing it as template
parameter to some dummy template, that the actually inst-counted
template declares as base class in order to instantiate it,
but that seems not to instantiate the variable, either:

int instanceCount(bool increase = false) {
static int ic = 0;
if (increase) ++ic;
return ic;
}

template <class I>
struct Counter
{ static const int trigger; };
template <class I>
int Counter<I>::trigger = instanceCount(true);

template <class C, const int* Ptr>
struct InstantiateCounter {};

template <class I>
struct myInstCounted : private InstantiateCounter<Counter<I>,
&Counter<I>::trigger>
{};

int main()
{
int i = sizeof(myInstCounted<double>);
std::cout << instanceCount() << '\n';
}

gives me a 0.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top