1

How can I separate header file from cpp file when I have template specialization ?

I have seen some posts on how to separate header from implementation for template by including cpp file at the end of header file. But this strategy does not work when I have to have template specialization.

here is my header file

#ifndef H_SUM_H
#define H_SUM_H

#include <map>
#include <string>

template <typename T>
double Sum(const T &source);

template <>
double Sum(const std::map<std::string, double> &source);

#ifndef CPP_SUM_CPP
#include "Sum.cpp"
#endif

#endif

and here is my implementation file

#ifndef CPP_SUM_CPP
#define CPP_SUM_CPP 
#include "Sum.h"

template <typename T>
double Sum(const T &source){//code//}

template <>
double Sum(const std::map<std::string, double> &source){//code//}

#endif

Two points :

1) If I remove the template specialization everything works well

2) When I include the cpp file (for the case with template specialization) the code

#ifndef CPP_SUM_CPP
#include "Sum.cpp"
#endif

is in grey color (in VC 2010) and is marked as a block code !!!

Any idea?

Thanks

Hamed
  • 757
  • 3
  • 10
  • 17
  • 2
    First, unless you seriously know what you're doing, stop trying to play hockey with preprocessor directives and [implement your templates in header files where they belong](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file?s=1|2.9910). From what I see, `Sum.cpp` shouldn't even *exist*. – WhozCraig Aug 30 '15 at 02:07
  • I am learning the templates in C++. I understand that in this case it is not necessary to have the cpp file. But this is part of a project and we are asked to write them in separate files. – Hamed Aug 30 '15 at 02:12
  • It's not a question of necessary; it's a question of even being appropriate. Whoever told you to do so seriously should read the link I provided and the ensuing answers therein. Ultimately the "strategy" shown, if it worked, is no better than just implementing in the header in the first place as far as the compiler is concerned. Thus the only benefit you get is something that is prone to break (which it has). Regardless, your `Sum.cpp` is *not* in your files built as part of your project, right? What is including `Sum.h` that fails? That looks more like an overload than a specialization. – WhozCraig Aug 30 '15 at 02:16
  • Thanks for your reply. I double checked and it seems that indeed it is not necessary to have cpp file in addition to hpp file for our project ! I follow you advise and include all in the header file. At least I ended up reading some interesting posts ... thanks for the link. – Hamed Aug 30 '15 at 02:22
  • Check this FAQ: https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl – EvgeniyZh Aug 30 '15 at 02:25

1 Answers1

0

Your source file can only have template<>, nothing inside <>, functions/methods cannot be partially specialized. Generic types can only exist in header files.

user1135541
  • 1,781
  • 3
  • 19
  • 41