Capture local variable in lambda with WaveShaper functionToUse?

quick question,
my current function is

    waveR.functionToUse = [](auto x) { return std::tanh(x * Math::pi); };

i want to do the following:

    waveL.functionToUse = [&cfg](auto x) { return std::tanh(x * Math::pi * cfg); };
    waveR.functionToUse =  [&cfg](auto x) { return std::tanh(x * Math::pi * cfg); };

cfg is reference to a float that is connected to my APVTS, i want to tighten the waveshaper by increasing this value but i cant implement it into my function.
I get the following error message:

Severity Code Description Project File Line Suppression State
Error (active) E0413 no suitable conversion function from lambda auto (auto x)->auto to float (*)(float) exists GummiClipDistortionV0_SharedCode E:\TsunamiBETA\GummiClipDistortionV0\Source\GCD_V0.cpp 42

The WaveShaper has an optional template parameter that specifies the type of the function to use. By default, this is a C function pointer, so it can only be assigned a stateless lambda (i.e. one with no captures).

If you want to set a function with captures, you’ll need to do something like:

struct Shaper
    float cfg;
    float operator() (float x) const { return std::tanh (x * Math::pi * cfg); }

// Then, declare your waveshapers like so
Waveshaper<float, Shaper> waveL, waveR;

// To set a new cfg value
waveL.functionToUse.cfg = waveR.functionToUse.cfg = cfg;

// Or alternatively
waveL.functionToUse = waveR.functionToUse = Shaper { cfg };
1 Like

perfect, thanks!