Origional code:
if (maxVol != 0)
std::transform(&spec[0], &spec[sampleSize], &spec[0], [maxVol] (float dB) -> float { return dB / maxVol; });
Lambda:
[maxVol] (float dB) -> float { return dB / maxVol; }
Replacement for lambda:
struct percent_of {
//that lambda captures maxVol by copy when constructed
percent_of(float maxVol) : maxVol(maxVol) {}
//the lambda takes a "float dB" and returns a float
float operator()(float dB) const { return dB / maxVol; }
private:
float maxVol;
};
Replacement for full code:
if (maxVol != 0)
std::transform(&spec[0], &spec[sampleSize], &spec[0], percent_of(maxVol));
However, this lambda is so simple, that it is now built into the standard library. Pre-C++11 had these exact same bits in boost
.
if (maxVol != 0) {
using std::placeholders::_1;
auto percent_of = std::bind(std::divides<float>(), _1, maxVol);
std::transform(&spec[0], &spec[sampleSize], &spec[0], percent_of);
}