audio - lambda function c++ 11 -
any idea how rewrite lambda function pre c++11?
if (maxvol != 0) std::transform(&spec[0], &spec[samplesize], &spec[0] , [maxvol] (float db) -> float { return db / maxvol; });
thanks
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 lambda:
struct percent_of { //that lambda captures maxvol copy when constructed percent_of(float maxvol) : maxvol(maxvol) {} //the lambda takes "float db" , returns float float operator()(float db) const { return db / maxvol; } private: float maxvol; };
replacement full code:
if (maxvol != 0) std::transform(&spec[0], &spec[samplesize], &spec[0], percent_of(maxvol));
however, lambda simple, built 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); }
Comments
Post a Comment