簡體   English   中英

使用枚舉來專門化模板

[英]Using an enum to specialise a template

我一直在使用帶有枚舉參數的模板來為我的代碼輸出提供專門的方法。

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

繼承怎么樣?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM