my situation is like this:
// abc.h
struct ABC{
ABC(const std::initializer_list<std::string>& il);
ABC(const std::vector<std::string>& il);
}
// abc.cpp
ABC::ABC(const std::initializer_list<std::string>& il){
// do stuff
}
ABC::ABC(const std::vector<std::string>& il){
// do exactly the same stuff - body is copy&paste from constructor above
}
// main.cpp
int main(){
ABC abc1 = {"str1","str2","str3"};
std::vector<std::string> v = {"str4","str5","str6"};
ABC abc2 = v;
}
I thought to declare a template constructor instead of those two and instantiate the two constructors for the input param types i need:
// abc.h
struct ABC{
template <typename T> ABC(const T& il);
}
template ABC::ABC<initializer_list<string>(const initializer_list<string>& il);
template ABC::ABC<vector<string>(const vector<string>& il);
and define the template body:
// abc.cpp
template <typename T> ABC::ABC(const T& il){
// do stuff
}
I got compiler error.
Question is: How can i reuse the code? or How both constructors can use the same code?