Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the max template instantation deph used during compilation

Tags:

c++

c++11

g++4.8

As the title says, is there any compiler logging settings which provides the max instantation deph reached by the compiler during compilation?

If the compilation exceds the max template deph (Which GCC's default value is 900 in C++11 mode), compilation fails. But what I need is to get the maximum template instantation depth which the compiler has reached during a successfull compilation.

like image 826
Manu343726 Avatar asked Sep 15 '13 12:09

Manu343726


People also ask

Are templates instantiated at compile time?

Templates are instantiated in the process of converting each translated translation unit into an instantiation unit. A translation unit is essentially a source file. A translated translation unit (try to say that three times fast) is the output from compilation without templates instantiated.

How the instantiation of function template happens?

When a function template is first called for each type, the compiler creates an instantiation. Each instantiation is a version of the templated function specialized for the type. This instantiation will be called every time the function is used for the type.

Are templates resolved at compile time?

All the template parameters are fixed+known at compile-time. If there are compiler errors due to template instantiation, they must be caught at compile-time!

Why it is necessary to instantiate a template?

In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).


1 Answers

g++ does have such an option, but it isn't enabled by default on kubuntu, for example.

The following is part of gcc/cp/tree.c from gcc-4.8.1 (and is therefore licensed under the GPL):

void
cxx_print_statistics (void)
{
  print_search_statistics ();
  print_class_statistics ();
  print_template_statistics ();
  if (GATHER_STATISTICS)
    fprintf (stderr, "maximum template instantiation depth reached: %d\n",
             depth_reached);
}

You can get those statistics when adding -fdump-statistics -fstats to your command line, but GATHER_STATISTICS has to be enabled at the time you compile gcc, so you will probably have to rebuild gcc in order to get the functionality you are looking for.

like image 108
us2012 Avatar answered Oct 14 '22 04:10

us2012