Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory model of template function inside non-template class?

Suppose I have:

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

I know that the compiler will generate a class A<T> for each different T defined in the code.

What if I have:

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

Would there be only one definition of class B but multiple overloads of f() for each different T it's called with?

like image 582
Shmoopy Avatar asked Sep 02 '14 08:09

Shmoopy


1 Answers

Yes, with every instantiation of f<T> there will be a definition of f() generated by compiler.
Depending on the compiler, the f() could be optimized due to inlining or it can just acquire that much space in the code segment.

However, I have seldom came across this kind of design where you have a non-static template member function (without any argument!) inside a non-template class.

like image 94
iammilind Avatar answered Sep 27 '22 21:09

iammilind