The memory model function of a template inside a class without a template?

Suppose I have:

template <typename T> class A { //Do something with T }; 

I know that the compiler will generate class A<T> for every other T defined in the code.

What if I have:

 class B { template <typename T> void f() { /* Do something with T */ } }; 

There would be only one definition of class B , but several f() overloads for every other T that it called with?

+6
source share
1 answer

Yes, with every instance of f<T> , the f() definition generated by the compiler will be created.
Depending on the compiler, f() may be optimized due to insertion, or it may just get so much space in the code segment.

However, I rarely came across such a design where you have a non-static function of a template member (without any arguments!) Inside a class without a template.

+1
source

Source: https://habr.com/ru/post/974680/


All Articles