Search code examples
c++sfinae

c++ SFINAE - fallback overload with ellipsis does not work


I'm writing function working with STL containers that have iterator.
And I'm trying to handle container that doesn't.

my template function:

template <typename T>
void    easyfind(...)
{
    throw std::invalid_argument("No iterator");
}

template <typename T>
typename T::iterator    easyfind(T& cont, int tofind)
{
    typename T::iterator    iter;

    iter = std::find(cont.begin(), cont.end(), tofind);
    if (iter == cont.end())
        throw std::out_of_range("Cannot find");
    return iter;
}

main.cpp:

// @ list
{
    std::list<int> L;
    std::list<int>::iterator iter;
    L.push_back(5);
    L.push_back(6);
    L.push_back(7);

    try
    {
        iter = easyfind(L, 7);
        std::cout << "found " << *iter << std::endl;
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

// @ stack ( no iterator )
{
    std::stack<int> S; 
    S.push(10);
    S.push(11);
    S.push(12);

    try
    {
        easyfind(S, 12);  // expect fallback case
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

I thought easyfind with stack would call void easyfind(...), but:

main.cpp:88:4: error: no matching function for call to 'easyfind'
                        easyfind(S, 12);
                        ^~~~~~~~
./easyfind.hpp:21:6: note: candidate template ignored: couldn't infer template argument 'T'
void    easyfind(...);
        ^
./easyfind.hpp:27:22: note: candidate template ignored: substitution failure [with T = std::stack<int>]: no type named 'iterator' in 'std::stack<int>'
typename T::iterator    easyfind(T& cont, int tofind);

The second ignore is what I expected, but i don't understand why it cannot call fallback function. What am I missing?


Solution

  • T is not used as a function argument so it can't deduce what T should be. In this case you could replace the varargs function with a variadic template:

    template <class... Args>
    void easyfind(Args&&...) // Now Args... can be deduced
    {
        throw std::invalid_argument("No iterator");
    }
    

    However, your current check excludes plain arrays, like int A[3];, and it also throws an exception in runtime instead of generating a clear error message at compile time. You could add a type trait to check if std::begin(container) is valid.

    #include <iterator>
    #include <type_traits>
    
    template<class T>
    struct has_iterator {
        static std::false_type test(...); // fallback
        
        template<class U>                 // matches if std::begin() is valid
        static auto test(const U& u) -> decltype(std::begin(u), std::true_type{});
    
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    
    template<class T>
    inline constexpr bool has_iterator_v = has_iterator<T>::value;
    

    Your easyfind could then use that type trait in a static_assert instead of adding a template overload and throwing in runtime.

    template <class T, class F>
    auto easyfind(T&& cont, F&& tofind) {
        // emit a clear compile time error message about the problem:
        static_assert(has_iterator_v<T>, "No iterator");
    
        auto iter = std::find(std::begin(cont), std::end(cont), std::forward<F>(tofind));
        if (iter == std::end(cont)) throw std::out_of_range("Cannot find");
        return iter;
    }
    

    Demo