Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11084

Re: calculate number of template instantiations

From Ruslan Mullakhmetov <tiabaldu@gmail.com>
Newsgroups comp.lang.c++
Subject Re: calculate number of template instantiations
Date 2011-10-25 19:42 +0400
Organization Aioe.org NNTP Server
Message-ID <j86ldo$rgv$1@speranza.aioe.org> (permalink)
References <j86c4d$vmq$2@speranza.aioe.org> <j86dpp$jop$1@dont-email.me>

Show all headers | View raw


On 2011-10-25 17:32:41 +0400, Victor Bazarov said:

> 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.
> 
> 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;
}

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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