2012-02-06 7 views
4

나는 내 코드에서 특수한 출력 방법을 제공하기 위해 enum 인수가있는 템플릿을 사용 해왔다.열거 형을 사용하여 템플릿 특수화

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]); 
    } 
}; 

이제 속도와 위치를 출력 할 DEVICE1과 유사한 센서를 추가하고 싶습니다.

여러 전문화를 설정하는 방법이 있습니까? 시도했습니다

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]); 
    } 
}; 

답변

3

상속은 어떨까요?

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> 
{ };