# Proper template declaration for dsp classes

**URL:** <https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279>\
**Category:** Audio Plugins\
**Created:** [February 6, 2018, 5:12pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279 "2018-02-06T17:12:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 6, 2018, 5:12pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/1 "2018-02-06T17:12:43Z")

</div>

For some time I have been using my own template version of the IIRFilter class so that I can pass floats or doubles to my processBlock(). Recently, I have begun to move over to the new dsp classes and wish to accomplish the same thing, which I thought would be easy since the dsp classes are already template classes. But I am having trouble figuring out the right way to declare the initial member variables.

My code is as follows;

```auto
// PluginProcessor.h

class PluginAudioProcessor : public AudioProcessor
{
public:

    PluginAudioProcessor();

    bool supportsDoublePrecisionProcessing() const override { return true; }

    void processBlock(AudioBuffer<float>& buffer, MidiBuffer& ) override
    {
    	dsp::AudioBlock<float> block(buffer);
	process(dsp::ProcessContextReplacing<float>(block));
    }

    void processBlock(AudioBuffer<double>& buffer, MidiBuffer& ) override
    {
	dsp::AudioBlock<double> block(buffer);
	process(dsp::ProcessContextReplacing<double>(block));
    }

private:

    template<typename T>
    void process(dsp::ProcessContextReplacing<T>);

    using GainProcessor = dsp::Gain<double>;
    using FilterProcessor = dsp::ProcessorDuplicator<dsp::IIR::Filter<double>, dsp::IIR::Coefficients<double>>;

    dsp::ProcessorChain<GainProcessor, FilterProcessor> chain;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginAudioProcessor)
}

//==============================================================================
// PluginProcessor.cpp

template<typename T>
void PluginAudioProcessor::process(dsp::ProcessContextReplacing<T> context)
{
    ScopedNoDenormals noDenormals;

    const float peak = (*pluginState.getRawParameterValue(Peak));
    const float peakHz = ((*pluginState.getRawParameterValue(PeakHz)) * 1000.0f);//convert to kilohertz
    const float peakQ = (*pluginState.getRawParameterValue(PeakQ));

    chain.get<0>().setGainLinear(*pluginState.getRawParameterValue(Trim));

    *chain.get<1>().state = *dsp::IIR::Coefficients<T>::makePeakFilter(getSampleRate(), peakHz, peakQ, peak);
    chain.process(context);
}

```

I need GainProcessor and FilterProcessor to be templates. But I have not figured out a way to declare them as such.

Any help is appreciated, or perhaps I need a different approach?

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [February 6, 2018, 7:03pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/2 "2018-02-06T19:03:57Z")

</div>

I think you wanted to write:

```auto
template <typename T>
using GainProcessor = dsp::Gain<T>;

template <typename T>
using FilterProcessor = dsp::ProcessorDuplicator<dsp::IIR::Filter<T>, dsp::IIR::Coefficients<T>>;

```

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 6, 2018, 7:40pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/3 "2018-02-06T19:40:27Z")

</div>

Thank you. That is what one would think should work, but it does not compile. At least, it does not compile in VS2017! The error is “only static data member templates are allowed”.

I know there must be a way to do this…

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [February 6, 2018, 7:47pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/4 "2018-02-06T19:47:11Z")

</div>

Could you copy-paste the complete compiler error? Depending on which line is producing that error, this could mean different things.

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 6, 2018, 8:01pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/5 "2018-02-06T20:01:29Z")

</div>

Below is the error from VS2017;

```auto
1>c:\aaxprojects2\Plugin\source\PluginProcessor.h(111): error C3376: 'PluginProcessor::chain': only static data member templates are allowed (compiling source file ..\..\Source\PluginEditor.cpp)
1>c:\aaxprojects2\Plugin\source\PluginProcessor.h(111): error C3376: 'PluginProcessor::chain': only static data member templates are allowed (compiling source file ..\..\Source\PluginProcessor.cpp)
1>..\..\Source\PluginProcessor.cpp(32): error C3245: 'PluginProcessor::chain': use of a variable template requires template argument list
1>c:\aaxprojects2\Plugin\source\PluginProcessor.h(111): note: see declaration of 'PluginProcessor::chain'

```

I am just not seeing how to form the syntax…your help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [February 6, 2018, 8:41pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/6 "2018-02-06T20:41:58Z")

</div>

⚠ Disclaimer: I am not familiar with these classes, so I might say something stupid. My solutions are only “correct” from the C++ POV, but there are maybe not correct for JUCE, or for what you want to achieve.

`chain` cannot be of type `dsp::ProcessorChain<GainProcessor, FilterProcessor>`, because `GainProcessor` and `FilterProcessor` are not types. `GainProcessor<double>`, `GainProcessor<float>`, `FilterProcessor<double>` and `FilterProcessor<float>` are types.

So the following would be valid:

```auto
dsp::ProcessorChain<GainProcessor<double>, FilterProcessor<double>> chain;

```

but I guess it’s not what you want.

If you want `chain` to be either a `dsp::ProcessorChain<GainProcessor<double>, FilterProcessor<double>>` or a `dsp::ProcessorChain<GainProcessor<float>, FilterProcessor<float>>`, then I guess you have to make `PluginAudioProcessor` a class template.

```auto
template <typename T>
class PluginAudioProcessor : public AudioProcessor
{
public:
    PluginAudioProcessor();

    bool supportsDoublePrecisionProcessing() const override { return true; }

    void processBlock(AudioBuffer<T>& buffer, MidiBuffer& ) override
    {
        dsp::AudioBlock<T> block(buffer);
        process(dsp::ProcessContextReplacing<T>(block));
    }

private:
    void process(dsp::ProcessContextReplacing<T>);

    using GainProcessor = dsp::Gain<T>;
    using FilterProcessor = dsp::ProcessorDuplicator<dsp::IIR::Filter<T>, dsp::IIR::Coefficients<T>>;

    dsp::ProcessorChain<GainProcessor, FilterProcessor> chain;
};

```

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 6, 2018, 10:48pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/7 "2018-02-06T22:48:51Z")

</div>

Thank you. But that doesn’t work because we need the float/double specializations for the template.

And this is why I posted here. The answer seems to be obvious, but it is not!

I have tried everything I can think of. So, I am at the point of needing help from those more knowledgeable.

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [February 6, 2018, 10:57pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/8 "2018-02-06T22:57:24Z")

</div>

> we need the float/double specializations for the template.

Of course! Stupid me 😅

Last idea: having two members (`floatChain` and `doubleChain`) and then passing the right member to `process()`.

```auto
// PluginProcessor.h

class PluginAudioProcessor : public AudioProcessor
{
public:

    PluginAudioProcessor();

    bool supportsDoublePrecisionProcessing() const override { return true; }

    void processBlock(AudioBuffer<float>& buffer, MidiBuffer& ) override
    {
        dsp::AudioBlock<float> block(buffer);
        process(dsp::ProcessContextReplacing<float>(block), floatChain);
    }

    void processBlock(AudioBuffer<double>& buffer, MidiBuffer& ) override
    {
        dsp::AudioBlock<double> block(buffer);
        process(dsp::ProcessContextReplacing<double>(block), doubleChain);
    }

private:
    template <typename T>
    using GainProcessor = dsp::Gain<T>;
    template <typename T>
    using FilterProcessor = dsp::ProcessorDuplicator<dsp::IIR::Filter<T>, dsp::IIR::Coefficients<T>>;
    template <typename T>
    using ProcessorChain = dsp::ProcessorChain<GainProcessor<T>, FilterProcessor<T>>;

    template <typename T>
    void process(dsp::ProcessContextReplacing<T>, ProcessorChain<T>& chain);

    ProcessorChain<double> doubleChain;
    ProcessorChain<float> floatChain;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginAudioProcessor)
}

//==============================================================================
// PluginProcessor.cpp

template<typename T>
void PluginAudioProcessor::process(dsp::ProcessContextReplacing<T> context, ProcessorChain<T>& chain)
{
    ScopedNoDenormals noDenormals;

    const float peak = (*pluginState.getRawParameterValue(Peak));
    const float peakHz = ((*pluginState.getRawParameterValue(PeakHz)) * 1000.0f);//convert to kilohertz
    const float peakQ = (*pluginState.getRawParameterValue(PeakQ));

    chain.get<0>().setGainLinear(*pluginState.getRawParameterValue(Trim));

    *chain.get<1>().state = *dsp::IIR::Coefficients<T>::makePeakFilter(getSampleRate(), peakHz, peakQ, peak);
    chain.process(context);
}

```

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 6, 2018, 11:50pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/9 "2018-02-06T23:50:17Z")

</div>

Great! That is working! Thank you for your help!

I still have a nagging suspicion that there may be another way to do it,… a whole different approach.

But for now, it compiles and runs.

Thank you!

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [February 7, 2018, 10:29am UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/10 "2018-02-07T10:29:03Z")

</div>

@McMartin’s code looks good to me.

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [February 7, 2018, 2:53pm UTC](https://forum.juce.com/t/proper-template-declaration-for-dsp-classes/26279/11 "2018-02-07T14:53:10Z")

</div>

Thank you. I wanted to make sure I was using best practices.

I think it would be nice to add similar capability to the demos for future reference.
