C ++ error: deduced conflicting types for the parameter 'T' string vs const char *

So, I am writing a simple template search function for the deque container. Here is the code:

    template <typename T>
    void searchInDequeFor(std::deque<T> Deque, T searchValue)
    {
        for(const auto & element : Deque)
        {
            if(Deque.empty())
            {
                std::cout << "Deque is empty, nothing to search for..." << "\n";
            }
            else if(element==searchValue)
            {
                std::cout << searchValue << " matches " << element << ", an element in the deque" << "\n";
            }
        }
    }

And so, as I call the main function:

        deque<string> myDeque={"apple", "banana", "pear", "blueberry"};
        searchInDequeFor(myDeque,"pear");

This is the error I get:

candidate template ignored: deduced conflicting types for parameter 'T' ('std::__1::basic_string<char>' vs. 'const char *')

Now I have tested this function with integers, floats, doubles, etc., and it works fine with these types, i.e. my template works (for these types). This makes me wonder why I get this error when the function clearly knows that I am passing strings of type, not of type const char * to deque. Any help would be brilliant. Thank!

+4
source share
2 answers

, std::string const char* (< - , "pear" ) - , T, .

, :

searchInDequeFor(myDeque,std::string("pear"));
+4

, , , T , .

template <typename T>
struct identity { typedef T type; };

template <typename T>
void searchInDequeFor(std::deque<T> Deque, typename identity<T>::type searchValue)

, std::deque<std::string> const char *, , T . , T std::string, , std::string, const char *.

+10

All Articles