0

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
}
  • @RemyLebeau There's plenty of duplicates for this that are not 12 years old. – super Jan 08 '21 at 18:24
  • well my question it is not the same thing? I need to just change in my present code – danielgabor Jan 08 '21 at 18:46
  • I need to make few changes in my code.. sorry if this is a old question, i am a young programmer, the others simillar questions didnt helped me :(... – danielgabor Jan 08 '21 at 18:51
  • Looks like good duplicates to me. You need to use SFINAE to determine if the object has a member function named find and enable/disable the overloads using `std::enable_if`. It's covered by the other questions. – super Jan 08 '21 at 19:35
  • other questions like? can you send me a link please? – danielgabor Jan 08 '21 at 19:37
  • Did you read the big blue box above your question with the reason yours was closed? There are 3 duplicates listed there. – super Jan 08 '21 at 19:38
  • yes, and nothing from there helped me, i have to complete my code as it is know.. – danielgabor Jan 08 '21 at 20:50

0 Answers0