I'm studing template instantiation process in C++ - Explicit instantiation declaration and definition. Having gone through few articles and documentations I realized that the explicit instantiation declaration is needed to avoid redundant template instantiation during compile time.
Is there a way to reproduce behavior with redundant template instances and find them somewhere to have a look?(what should I do to see redundant template instances?)
I tried to compile the code below with and without external template declaration. But when I decompiled .obj
files(to decompile used this https://dogbolt.org/?id=16d5a9f7-b73e-4de9-b4d1-d121d70e33b1#Hex-Rays=97&Ghidra=473&BinaryNinja=91), I didn't find a place where ATest
would be instantiated twice and more(perhaps I missed it because I don't understand the decompiled code pretty much). I'm not even sure that it is correct place.
//TestA.h
template <typename T, T val>
struct ATest {
ATest();
T foo();
T bar();
};
//extern template struct ATest<int, 55>;
//extern template ATest<int, 55>::ATest(void);
//extern template int ATest<int, 55>::foo(void);
//extern template int ATest<int, 55>::bar(void);
//TestA.cpp
#include <iostream>
#include "TestA.h"
template <class T, T val>
T ATest<T, val>::foo(void) {
std::cout << "Hello from foo" << std::endl;
return val;
}
template <class T, T val>
T ATest<T, val>::bar(void) {
std::cout << "Hello from bar" << std::endl;
return val;
}
template <class T, T val> ATest<T, val>::ATest(void) {
std::cout << "Hello from A()" << std::endl;
}
template struct ATest<int, 55>;
template ATest<int, 55>::ATest(void);
template int ATest<int, 55>::foo(void);
template int ATest<int, 55>::bar(void);
//MainA.cpp
#include "TestA.h"
int main()
{
ATest<int, 55> a;
ATest<int, 55> b;
ATest<int, 55> c;
ATest<int, 55> d;
ATest<int, 55> e;
ATest<int, 55> f;
ATest<int, 55> g;
return 0;
}
Any ideas would be really appreciated.
Update My question has been associated with this Why can templates only be implemented in the header file? I've read the question and 3 first answers but it is not related to my question.
My question is not about how to use temapltes in headers and why they can be implemeted in headers in C++(it is from the post pointed out above). I'm asking about compilation result of the template instances. It is rather more related to C
language than what is in that post. That's why stackoverflow suggested me to choose C
tag I believe.
I'm also not very experienced in C++
yet, so my question may confuse. Sorry for this then.
Update2 Ok. The second post using extern template (C++11) where my post is accosiated with explains a lot with example. But It doesn't say how and what should be decompiled to be able to see the difference. So It doesn't answer my question.
Update3(My solution) Reading this post I've realized how to achive this behavior Explicit template instantiation - when is it used?
I also misinterpreted the concept with redundant instantiation. Probably I still 100% don't understand it, but I've figured out that to get redundant instances we need to skip both: Explicit instantiation definition and declaration and create template definition in header.