Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calculate number of template instantiations

Reply
Thread Tools

calculate number of template instantiations

 
 
Ruslan Mullakhmetov
Guest
Posts: n/a
 
      10-25-2011
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate number of template instantiations Ruslan Mullakhmetov C++ 7 10-26-2011 09:10 AM
Mirroring template instantiations Imre C++ 8 10-26-2006 01:59 PM
extern usage for template instantiations sks C++ 3 12-03-2005 01:51 PM
Can different instantiations of the same template can access others' private member? PengYu.UT@gmail.com C++ 1 10-19-2005 05:52 PM
Re: STL list and template instantiations mpichini C++ 0 07-01-2003 05:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57