I want to execute the specific method of the template if a container has his own find
method. For example for set
it will execute the first. And for vector
the second. Because vector does not have a find
method.
namespace tools {
template<class TSource, class Ty>
auto exists_in(const TSource &source, const Ty &item)
->decltype(source->find())
{
return source.find() != source.end();
}
template<class TSource, class Ty>
auto exists_in(const TSource &source, const Ty &item)
{
return std::find(source.begin(), source.end(), item)
!= source.end();
}
}
int main()
{
std::set<int> vector= { 1, 2, 3 };
std::vector<int> set = { 1, 2, 3 };
std::cout<<tools::exists_in(vector, 1);///should execute the first **tools::exists_in** method
std::cout<<tools::exists_in(set, 1);///should execute the second **tools::exists_in** method
}