<p>Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class?</p>

<p>Here's a simple example of what I would want to write:</p>

<pre><code>template&lt;class T&gt;
std::string optionalToString(T* obj)
{
    if (FUNCTION_EXISTS(T-&gt;toString))
        return obj-&gt;toString();
    else
        return "toString not defined";
}
</code></pre>

<p>So, if <code>class T</code> has <code>toString()</code> defined, then it uses it; otherwise, it doesn't. The magical part that I don't know how to do is the "FUNCTION_EXISTS" part.</p>

This is what type traits are there for. Unfortunately, they have to be defined manually. In your case, imagine the following:
```
```
template <typename T>
struct response_trait {
    static bool const has_tostring = false;
};

template <>
struct response_trait<your_type_with_tostring> {
    static bool const has_tostring = true;
}
```
```