How to find how many instances of templates are created upon compilation

Hi,

I have this code and want to find how many instances of templates are generated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<size_t A>
struct Wrap_
{
    template<size_t ID, typename TDummy=void>
    struct imp
    {
        constexpr static size_t value = ID + imp<ID-1>::value;
     };
    template<typename TDummy>
    struct imp<0, TDDummy>
    {
        constexpr static size_t value = 0;
     };

     template<size_t ID>
     constexpr static size_t value = imp<A + ID>::value;

};
int main()
{
      auto res = Wrap_<3>::value<2>;
}


As far as I understand it, there will be these instantiations:

Wrap_<3>::value<2> // A+ID = 5
Wrap_<3>::imp<5>
Wrap_<3>::imp<4>
Wrap_<3>::imp<3>
Wrap_<3>::imp<2>
Wrap_<3>::imp<1>
Wrap_<3>::imp<0>

Now, if we change the code by not nesting imp inside Wrap_ then presumably we obtain less instantiations (so says the author)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   template<size_t ID>
    struct imp
    {
        constexpr static size_t value = ID + imp<ID-1>::value;
     };
    template<>
    struct imp<0>
    {
        constexpr static size_t value = 0;
     };
     template<size_t A>
     struct Wrap_
      {
         template<size_t ID>
         constexpr static size_t value = imp<A+ID>::value;  
      };


how many instantiations do we get here?
one for each call to imp<X>::value?


Regards,
Juan
Logically created, or actually created?

Because by the time the optimiser has resolved everything, it's all gone in a puff of logic.

$ g++ -std=c++14 foo.cpp
$ ./a.out 
15
$ nm -C a.out 
0000000000004010 B __bss_start
0000000000004150 b completed.8060
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004000 W data_start
00000000000010f0 t deregister_tm_clones
0000000000001160 t __do_global_dtors_aux
0000000000003d90 d __do_global_dtors_aux_fini_array_entry
0000000000004008 D __dso_handle
0000000000003d98 d _DYNAMIC
0000000000004010 D _edata
0000000000004158 B _end
00000000000012c8 T _fini
00000000000011a0 t frame_dummy
0000000000003d80 d __frame_dummy_init_array_entry
00000000000021a4 r __FRAME_END__
0000000000003f98 d _GLOBAL_OFFSET_TABLE_
0000000000001231 t _GLOBAL__sub_I_main
                 w __gmon_start__
0000000000002008 r __GNU_EH_FRAME_HDR
0000000000001000 t _init
0000000000003d90 d __init_array_end
0000000000003d80 d __init_array_start
0000000000002000 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
00000000000012c0 T __libc_csu_fini
0000000000001250 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
00000000000011a9 T main
0000000000001120 t register_tm_clones
00000000000010c0 T _start
0000000000004010 D __TMC_END__
00000000000011e4 t __static_initialization_and_destruction_0(int, int)
                 U std::ostream::operator<<(unsigned long)@@GLIBCXX_3.4
                 U std::ios_base::Init::Init()@@GLIBCXX_3.4
                 U std::ios_base::Init::~Init()@@GLIBCXX_3.4
0000000000004040 B std::cout@@GLIBCXX_3.4
0000000000002004 r std::piecewise_construct
0000000000004151 b std::__ioinit
                 U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)@@GLIBCXX_3.4
C++ Insights (after fixing the compile time errors) can show template instantiations:
https://cppinsights.io/s/0bb08e15 (Ctrl+Enter to run C++ Insights)
Topic archived. No new replies allowed.