Consolidating two functions
Hello, all.
Let's say there are two class member functions that do the same thing but have different argument types.
Code:
bool some_class::isX(string S) const
{
return stl_set_object.end() != stl_set_object.find(S)
}
bool some_class::isX(const char* S) const
{
return stl_set_object.end() != stl_set_object.find(S)
}
So I consolidated this with a template to:
Code:
template <class T>
bool isX(T S) const
{
return stl_set_object.end() != stl_set_object.find(S)
}
But what if I wanted to pass a string object as a reference and leave character pointers alone.
So the first function would be modified to:
Code:
bool some_class::isX(string& S) const //note the addressof operator here
{
return stl_set_object.end() != stl_set_object.find(S)
}
Would there still be a way to consolidate the two functions (one with string reference argument and another with const char*) into one using a template?
It strikes me as counter-intuitive, but I was just curious to know the flexibility of the STL and c++ with regard to this.