Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non datatype template parameter, more specialization generated?

My Code is:

#include <iostream>

using namespace std;


template <typename T, int X>

class Test
{
   private:
    T container[X];
   public:
    void printSize();

};
template <typename T, int X>
void Test<T,X>::printSize()
{
    cout <<"Container Size = "<<X <<endl;
}


int main()
{
    cout << "Hello World!" << endl;
    Test<int, 20> t;
    Test<int, 30> t1;

    t.printSize();
    t1.printSize();
    return 0;
}

Question:

  1. How many specialization will get generated?. If I understand correctly , it generates two specializations one is for <int, 20> and another is for <int, 30>. Kindly Correct if my understanding is wrong?
  2. Is there any way to see/check the number of specializations generated by any reverse engineering?
like image 586
Whoami Avatar asked Apr 18 '13 08:04

Whoami


2 Answers

There are not specializations here, only instantiations (this questions explains the difference). This code generates two instantiations of the class template Test.

like image 199
Björn Pollex Avatar answered Nov 12 '22 06:11

Björn Pollex


1) yes, two instantiations will be generated by the compiler, but the linker might merge functions with identical generated code (using whole program optimization e.g.), which is a cute way to reduce code bloat.

2) see this question where it is explained how gcc can generate template instantiation output.

like image 2
TemplateRex Avatar answered Nov 12 '22 06:11

TemplateRex