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 <class Out, class In >
auto integrate( std::function<Out(In)> const & f,
In xmin, In xmax, int nSteps)
-> 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<temperature> stdBlackBodyTemp = 2857.0*kelvin;
auto luminousEfficiency = quantity<luminous_flux>(682.0*lumen) / quantity<power>(1.0 * watt);
//Integration range
quantity<length> minWavelength(150.0 * nano * metre);
quantity<length> maxWavelength(600.0 * nano * metre);
//Get type for spectral_differential_current
quantity<length> fakeLength(300.0 * nano * metre);
typedef decltype(pmt_radiant_sensitivity(fakeLength)
* plancklaw(fakeLength, stdBlackBodyTemp)) sdctype;
//Define spectral_different_current function
std::function< sdctype(quantity<length>)>
spectral_differential_current = [&stdBlackBodyTemp](quantity<length> 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?
minWavelength
instead of introducingfakeLength
. Since there could be multiplepmt_radiant_sensitivity
andplancklaw
with different return types, I'm not sure what you'd expect without giving the input type – anddecltype
has been constructed to take a standard expression, not some new syntax that only gets types and functions. \$\endgroup\$