Matei Radu Matei Radu - 6 months ago 41
C++ Question

return NULL from a C++ template

I have to create a template function that searches an element in a map variable. If the element exists, the function must return it. Otherwise i must return NULL.

template <class K, class E> E dictionary<K,E>::research(K key){

//map<K,E> elements;

return elements.find(key)->second;
return NULL;

Since the return type is E, returning NULL gives me always an error. How can I make this work? Or I have to structure my function differently?

This is a school homework assignment and i must return NULL, no alternatives. I'd personally do differently if I could.


The design of this function is incorrect. It is perfectly possible that NULL is simply incompatible with the type E. For instance, suppose that E is a struct, or a std::string and so on.

This is a school homework assignment and I must return NULL, no alternatives.

Either your homework assignment is incorrect, or you have misunderstood its requirements. It is possible that you are expected to return a pointer to E?