C++ What is the best way to separate declaration and definition of class when using templates -


i have searched lot on topic. came across 3 methods. suppose have 3 files, main.cpp, declaration header file , definiton cpp file. first method suggests create temporary opject in cpp file. second method suggests include both cpp , header files in our main file. third method suggests include cpp file in header file.

now question: 1 best method ? saying best mean both in performance , @ proper code styling. there other better methods?

templates should defined inside class body. preferred solution, because it's simple, clear , allows avoid lot of unnecessary typing.

consider following class:

template <charencoding encoding> class unicodeconverter { protected:     template <typename input_char_type, typename output_char_type>     class internalhelper     {     public:         void function_a()         {             //do         }     };      //... }; 

now imagine, have write if wanted have separate definition file:

template <charencoding encoding> template <typename input_char_type, typename output_char_type> void unicodeconverter<encoding>::internalhelper<input_char_type, output_char_type>::function_a() {   //do } 

that not pretty nor readable, wouldn't agree?

there situations, definition must separated (for example, 2 templates, depend on each other), in cases: avoid it!

the rule of placing implementation in separate .cpp files, not apply templates. c++ standard library header-based, extensively uses templates everything.

also, note, sample code real sample, taken real app (slightly modified). , imagine, not complicated template have in project.


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

html - grunt SVG to webfont -

c# - SharpSsh Command Execution -