Why is it trying to find the destructor twice?

I tried the following code snippet:

GeneralTemplate.h

#ifndef _GENERATEMPLATE_H_ #define _GENERATEMPLATE_H_ #include <iostream> template <class T> class GeneralTemplate { public: GeneralTemplate(); GeneralTemplate(const GeneralTemplate &g); ~GeneralTemplate(); GeneralTemplate& operator= (GeneralTemplate const& g); template <class M> void arbitraryFunction(const M &m); }; #endif 

main.cpp

 #include "GeneralTemplate.h" #include <iostream> int main() { GeneralTemplate<int> gInt; gInt.arbitraryFunction(2.3); return 0; } 

Note that I do not have an implementation for member functions of the class template. But it's not a problem. I know how to do it! If I try to compile main.cpp, I should get a binding error and what I get. The question is why it is trying to find the destructor twice (the last two lines of the error are below).

 $g++ main.cpp /tmp/cckrdPCs.o: In function `main': main.cpp:(.text+0x13): undefined reference to `GeneralTemplate<int>::GeneralTemplate()' main.cpp:(.text+0x34): undefined reference to `void GeneralTemplate<int>::arbitraryFunction<double>(double const&)' main.cpp:(.text+0x45): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()' main.cpp:(.text+0x61): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()' collect2: ld returned 1 exit status 
+6
source share
1 answer

This is most likely due to exception safety. If arbitraryFunction throws an exception, the stack needs to be unwound, that is, gInt needs to be destroyed earlier. Since this all happens in main , where no unwinding takes place anymore, it is doubtful whether he really needs two calls to the destructor ... but the behavior you observe is not quite off the wall.

+8
source

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


All Articles