Determining function return types for std::function I'm using the boost::units library (boost::units) and trying to make use of auto and decltype to deal with all the complicated types it produces.

I have a templated function to integrate a mathematical function (which might accept x values with units such as 1*metre) between two points:

template &lt;class Out, class In &gt;
auto integrate( std::function&lt;Out(In)&gt; const &amp; f,
In xmin, In xmax, int nSteps)
-&gt; decltype( f(xmin)  * xmin  )


Now I want to use this with some functions that have big ugly return types. I have written this:

//Standard quantities
quantity&lt;temperature&gt; stdBlackBodyTemp = 2857.0*kelvin;
auto luminousEfficiency = quantity&lt;luminous_flux&gt;(682.0*lumen) / quantity&lt;power&gt;(1.0 * watt);

//Integration range
quantity&lt;length&gt; minWavelength(150.0 * nano * metre);
quantity&lt;length&gt; maxWavelength(600.0 * nano * metre);

//Get type for spectral_differential_current
quantity&lt;length&gt; fakeLength(300.0 * nano * metre);

typedef decltype(pmt_radiant_sensitivity(fakeLength)
        * plancklaw(fakeLength, stdBlackBodyTemp)) sdctype;

//Define spectral_different_current function
std::function&lt; sdctype(quantity&lt;length&gt;)&gt;
    spectral_differential_current = [&amp;stdBlackBodyTemp](quantity&lt;length&gt; l)
    {
        return
            pmt_radiant_sensitivity(l) * plancklaw(l, stdBlackBodyTemp );
    };

//Integrate over spectral_differential_current
auto differential_current = integrate( spectral_differential_current,
        minWavelength, maxWavelength, 100);


To define my std::function spectral_differential_current which I'm going to give to integrate I need to know the return type. The only way I have found to get this is by using the function on some random value (fakeLength) and then using decltype. It seems odd that I have to introduce a variable which isn't actually used just to get a type. Is there a better way of approaching this?
I'm not sure why you need the typedef in the first place. Is there anything wrong with auto spectral_differential_current = ...?

On a similar note, why do you want to require that f is an std::function, disallowing reasonable calls like integrate(sin, 0, 2, 10)? Most code of this type can happily accept anything that can be called:

template &lt;typename F, typename In&gt;
auto integrate(F f, In xmin, In xmax, int nSteps)
-&gt; decltype(f(xmin)  * xmin)


The compiler will tell you whether a given f is OK. Usually by not finding a suitable integrate function – SFINAE.

Also note the use of typename – that's just a stylistic thing of course, but e.g., double is not a class, so I prefer not to say class for template parameters that would accept a double or a plain function pointer.