Q:

Template function: perform conversion based on typename

Is there a way to achieve something like this in C++:
template<typename T>
T function()
{
    std::string result = getRes();
    // if (type == string)
        return result;
    // else if (type == numeric)
        return std::stoul(result, nullptr);
}

Assuming the result variable type is always known (in this case string).  The return type is defined by the template in the function call, e.g.:
int x = function<int>();

I know that there are things like std::is_integral or std::is_same.  With those functions I could determine the type of T. However it does not seem to work, because it cannot be evaluated during compile time and therefore the compiler throws errors. 

A:

You can use a stringstream:
#include <sstream>

template<typename T>
T function()
{
    std::string result = getRes();
    stringstream ss;
    ss << result;
    T value;
    ss >> value;
    return value;
}

