Separate definitions of variation patterns

(last question below)

I recently asked a question about how I would fix linker errors (about duplicate characters regarding multiple void template definitions.

Since I used functions in several source files, I was offered to use the inline to allow ads in the header or to place ads in a compiled source file.

After I realized that inline had some bad consequences, I put my ads in the source file.

Now it was good, with the exception of variation patterns:

 template<typename T, typename... Args> void cleanup(T *t, Args&&... args); 

I found some obvious solutions, but not variation patterns - use a .tpp file (but it started declaring duplicate characters again) or save the original file and add explicit instances.

But void cleanup has the potential to use hundreds of combinations of parameters, so I donโ€™t want to explicitly create all instances.

Question: So, how would I go or

  • Saving variational template definitions in the source file, OR
  • Including definitions in a .tpp file without getting duplicate characters and ultimately avoiding using inline ?

Examples of duplicate / undefined character errors for declaring .tpp and placing the above template definition in the source file, respectively.

 duplicate symbol __Z7cleanupI10SDL_WindowJEEvPT_DpOT0_ in: CMakeFiles/Game.dir/Game/main.cc.o CMakeFiles/Game.dir/Game/RichTools/rtexture.cc.o 

_

 Undefined symbols for architecture x86_64: "void cleanup<SDL_Renderer, SDL_Window*&>(SDL_Renderer*, SDL_Window*&&&)", referenced from: cleanQuit() in main.cpp.o ld: symbol(s) not found for architecture x86_64 
+6
source share
1 answer

Advice in response to your first question: to make an inline or go to the source file was only about your fully specialized functions - with an empty list of template <> parameters.

So do this:

Your header file:

 // This shall be in header - it is not full specialization: template<typename T, typename... Args> void cleanup(T *t, Args&&... args){ //Cleanup the first item in the list cleanup(t); //Recurse to clean up the remaining arguments cleanup(std::forward<Args>(args)...); } // These shall be only declared here, and implemented in source file, // treat fully specialized function templates as regular functions template<> void cleanup<SDL_Window>(SDL_Window *win); template<> void cleanup<SDL_Renderer>(SDL_Renderer *ren); template<> void cleanup<SDL_Texture>(SDL_Texture *tex); 

Original file:

 template<> void cleanup<SDL_Window>(SDL_Window *win){ if (!win){ return; } SDL_DestroyWindow(win); } template<> void cleanup<SDL_Renderer>(SDL_Renderer *ren){ if (!ren){ return; } SDL_DestroyRenderer(ren); } template<> void cleanup<SDL_Texture>(SDL_Texture *tex){ if (!tex){ return; } SDL_DestroyTexture(tex); } template<> void cleanup<SDL_Surface>(SDL_Surface *surf){ if (!surf){ return; } SDL_FreeSurface(surf); } 
+2
source

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


All Articles