The template parameter is not of the datatype type, is additional 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:

  • How much specialization will be created ?. If I understand correctly, it generates two specializations: one for <int, 20>, and the other for <int, 30>. Please, right, if my understanding is wrong?
  • Is there any way to see / check the number of specializations generated by any reverse engineering?
+5
source share
4 answers

1) yes, two instances will be generated by the compiler , but the linker can combine functions with identical generated code (for example, using all the program optimization), which is a nice way to reduce code bloat.

2) . , gcc .

+2

, ( ). Test.

+3

1) ?. , : . , , ?

, .

2) / ?

, . , , .

0

a) In your example, 2 instances of specialization are created.

b) There is no built-in method to support the number of specializations generated for a class. If in your project you can add a static quantity. If you want, you can write your own link counting mechanism for your class. Increase the static score in our constructor.

static int created = 0;
static int alive = 0;
class Test
{
counter()
    {
        created++;
        alive++;
    }
~counter()
{
  created--;
}
//Rest of class
};
0
source

All Articles