Template function specialization with code duplication I'm working on a libcurl binding for NodeJS, and to get info from a curl handler, using curl_easy_getinfo, I've created a template function, the issue, is that the result of the call can be a null pointer.

To make sure this was correctly addressed, I've created a template specialization that handle the edge case:

template&lt;typename ResultType, typename v8MappingType&gt;
v8::Handle&lt;v8::Value&gt; Curl::GetInfoTmpl( const Curl &amp;obj, int infoId )
{
    v8::HandleScope scope;

    ResultType result;

    CURLINFO info = (CURLINFO) infoId;
    CURLcode code = curl_easy_getinfo( obj.curl, info, &amp;result );

    if ( code != CURLE_OK )
        return Curl::Raise( "curl_easy_getinfo failed!", curl_easy_strerror( code ) );

    return scope.Close( v8MappingType::New( result ) );
}

//The null pointer is only possible when the value returned is a c string
template&lt;&gt; //workaround for null pointer, aka, hack. 
v8::Handle&lt;v8::Value&gt; Curl::GetInfoTmpl&lt;char*,v8::String&gt;( const Curl &amp;obj, int infoId )
{
    //duplicated code
    v8::HandleScope scope;

    char *result;

    CURLINFO info = (CURLINFO) infoId;
    CURLcode code = curl_easy_getinfo( obj.curl, info, &amp;result );

    if ( !result ) { //null pointer
        return v8::String::New( "" );
    }

    if ( code != CURLE_OK )
        return Curl::Raise( "curl_easy_getinfo failed!", curl_easy_strerror( code ) );

    return scope.Close( v8::String::New( result ) );
}


Is this the correct way to handle that, or are there better ways to do so?

The full code is on GitHub.
The check is better factored into a specialization on a traits class. This allows you to have one implementation of the overall logic flow, and just specialize the part that changes. I haven't fully researched the implications of this approach, but I expect that due to the always true/false nature of the check (for most given types), that it will be easily optimized away.

It would look something like this:

// traits class to determine whether to do the check
template &lt;typename&gt; struct ResultCanBeNull : std::false_type {};
template &lt;&gt; struct ResultCanBeNull&lt;char*&gt; : std::true_type {};

template&lt;typename ResultType, typename v8MappingType&gt;
v8::Handle&lt;v8::Value&gt; Curl::GetInfoTmpl( const Curl &amp;obj, int infoId )
{
    v8::HandleScope scope;

    ResultType result;

    CURLINFO info = (CURLINFO) infoId;
    CURLcode code = curl_easy_getinfo( obj.curl, info, &amp;result );

    // use the traits class to filter in or out the null pointer check
    if ( ResultCanBeNull&lt;ResultType&gt;::value &amp;&amp; !result ) { //null pointer
        return v8::String::New( "" );
    }

    if ( code != CURLE_OK )
        return Curl::Raise( "curl_easy_getinfo failed!", curl_easy_strerror( code ) );

    return scope.Close( v8MappingType::New( result ) );
}


You could also implement the traits support as a method instead, in case it's not valid to call !result on instances of other types that are reasonable for ResultType.

template &lt;typename ResultType&gt;
struct ResultTraits {
    bool IsNull(const ResultType&amp;) { return false; }
};
template&lt;&gt; bool ResultTraits&lt;char const*&amp;&gt;::IsNull(char const*&amp; pchar) {
    return !pchar;
};

: : :

    if (ResultTraits&lt;ResultType&gt;::IsNull(result)) {
        return v8::String::New( "" );
    }