C++ 使用枚举专门化模板
我一直在使用带有enum参数的模板,为代码的输出提供专门的方法C++ 使用枚举专门化模板,c++,templates,C++,Templates,我一直在使用带有enum参数的模板,为代码的输出提供专门的方法 template <Device::devEnum d> struct sensorOutput; template<> struct sensorOutput <Device::DEVICE1> { void setData(Objects& objs) { // output specific to DEVICE1 // output
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]);
}
};
模板
结构传感器输出;
模板
结构传感器输出
{
void setData(对象和对象)
{
//特定于设备1的输出
//输出速度
objs.set(速度,vel[设备::设备1]);
//输出位置
对象集(位置,位置[设备::设备1]);
}
};
模板
结构传感器输出
{
void setData()
{
//特定于设备2的输出
//输出高度
objs.set(高度、高度[设备::设备2]);
}
};
我现在想添加另一个类似于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]);
}
};
模板
结构传感器输出
结构传感器输出
{
void setData()
{
//特定于设备1和设备3的输出
//输出速度
对象集(速度,水平[d]);
//输出位置
对象集(位置,位置[d]);
}
};
继承如何
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>
{ };
模板
结构传感器输出速度和位置
{
void setData()
{
//特定于设备1和设备3的输出
//输出速度
对象集(速度,水平[d]);
//输出位置
对象集(位置,位置[d]);
}
}
模板
结构传感器输出:公共传感器输出速度和位置
{ };
模板
结构传感器输出:公共传感器输出速度和位置
{ };