What's the best way to define constants? #define, static const, local const

What’s the best way to define constants in a dsp context?(for example a wavetable size integer value or parameter ID strings). Is it better to use a #define macro(I read somewhere saying this is faster)? or a global static const? or a local member variable const? Does this even matter or am I just putting my energy in trying useless optimizations?
To be more specific, my context is this. I have a SynthesizerVoice and in its processBlock it interpolates the wavetable array, and atm a WAVETABLE_SIZE macro is used in the interpolation process(for the fmod processes etc).

(I read somewhere saying this is faster)

It’s not. Even not declaring something constant is (probably) going to generate the same assembly. proof.

The modern C++ language feature for doing this is constexpr. Best practice is to avoid the preprocessor at all costs. When you use global constants, the idea is to avoid polluting the global namespace, e.g.

#define PI 3.14159 // bad, PI is now everywhere 
#define CONST_PI 3.14159 // less bad 
namespace consts {
    static constexpr double PI = 3.14159; // pretty good way to do it 
}

With WAVE_TABLE_SIZE I’d personally put it in the wavetable class/namespace (however you’re keeping your code modular) so it would look like this:

class WaveTable {
public:
// ...

private:
//...
    static constexpr size_t WAVETABLE_SIZE = 1024; // or whatever
};
5 Likes