Using an enumeration to specialize a template

I use a template with an enum argument to provide specialized methods for output from my code.

template <Device::devEnum d> struct sensorOutput; template<> struct sensorOutput <Device::DEVICE1> { void setData(Objects& objs) { // output specific to DEVICE1 // output velocity objs.set(VELOCITY, vel[Device::DEVICE1]); // output position objs.set(POSITION, pos[Device::DEVICE1]); } }; template <> struct sensorOutput <Device::DEVICE2> { void setData() { // output specific to DEVICE2 // output altitude objs.set(ALTITUDE, alt[Device::DEVICE2]); } }; 

Now I want to add another sensor, similar to DEVICE1, which will output speed and position.

Is there a way to establish several specializations? I tried

 template <> struct sensorOutput <Device::DEVICE1 d> struct sensorOutput <Device::DEVICE3 d> { void setData() { // output specific to DEVICE1 and DEVICE3 // output velocity objs.set(VELOCITY, vel[d]); // output position objs.set(POSITION, pos[d]); } }; 
+4
source share
1 answer

What about inheritance?

 template<Device::devEnum d> struct sensorOutputVeloricyAndPosition { void setData() { // output specific to DEVICE1 and DEVICE3 // output velocity objs.set(VELOCITY, vel[d]); // output position objs.set(POSITION, pos[d]); } } template<> struct sensorOutput<Device::DEVICE1> : public sensorOutputVeloricyAndPosition<Device::DEVICE1> { }; template<> struct sensorOutput<Device::DEVICE3> : public sensorOutputVeloricyAndPosition<Device::DEVICE3> { }; 
+3
source

Source: https://habr.com/ru/post/1394994/


All Articles