Having a special C++ template class variant where the template parameter is omitted
09:08 05 Jan 2026

I have a template class with one parameter and a number of small member functions.

template
class SFRegT {
    public:

    void Reset() const {
        *(volatile uint32_t*)mRegAddr = 0;
    }

    void Set(uint32_t  Value) const {
        *(uint32_t*)mRegAddr = Value;
    }

    uint32_t Get() const {
        return *(volatile uint32_t*)mRegAddr;
    }

    uint32_t  operator()() const { return Get(); }

    void setBit(const uint16_t BitPos) const {
        setSFRBit(mRegAddr, BitPos);
    }
    // and some more member
};

Now I need another similar class where my parameter is no longer a template parameter, but a constexpr member variable.

template<>
class SFRegT {
    public:
    constexpr SFReg(uintptr_t RegAddr) :
       mRegAddr(RegAddr) {
    }   
    // the same member functions as above
    private:
        const uintptr_t mRegAddr;
};

Is there a way to avoid the redundancy? So, you have only one template class with two variants: one where the constructor and member variable are omitted if the template parameter is present, and the other where the constructor and local parameter are present if the template parameter is omitted.

c++ templates redundancy