0

Пытаюсь написать класс с шаблонными методами. Как сделать так, чтобы избавиться от явной реализации и дублированного кода?

class Attribute final
{
public:

    Attribute();
    Attribute(ViInt32   value, bool rEnable, bool wEnable);
    Attribute(ViInt64   value, bool rEnable, bool wEnable);
    Attribute(ViReal64  value, bool rEnable, bool wEnable);
    Attribute(ViBoolean value, bool rEnable, bool wEnable);

    ~Attribute();

    template<typename T>
    void get(T value) const;

    template<typename T>
    void set(T value);

    bool isReadingEnable();
    bool isWritingEnable();

private:

    template<typename T>
    void Init(T value, bool rEnable, bool wEnable);

    bool readingEnable;
    bool writingEnable;

    std::any attribute;
};

template<>
inline void Attribute::set(ViInt32 value)
{
    attribute = value;
}

template<>
inline void Attribute::set(ViInt64 value)
{
    attribute = value;
}

template<>
inline void Attribute::set(ViReal64 value)
{
    attribute = value;
}

template<>
inline void Attribute::set(ViBoolean value)
{
    attribute = value;
}

template<>
inline void Attribute::get(ViInt32* value) const
{
    *value = std::any_cast<ViInt32>(attribute);
}

template<>
inline void Attribute::get(ViInt64* value) const
{
    *value = std::any_cast<ViInt64>(attribute);
}

template<>
inline inline void Attribute::get(ViReal64* value) const
{
    *value = std::any_cast<ViReal64>(attribute);
}
3
  • @user7860670 исправил код в вопросе. Не подскажите, как решить задачку ?
    – Killlin
    21 мая 2020 в 16:24
  • @user7860670 if constexpr появилась только в 17 стандарде (и я не уверен что оно сдесь подойдет). Можно также через std::enable_if 21 мая 2020 в 16:37
  • 1
    Можно сделать аналогично ru.stackoverflow.com/questions/1122451/… 21 мая 2020 в 17:49

1 ответ 1

1

Не соображу, в чем проблема?

class Attribute final
{
public:

    Attribute();
    template<class T>
    Attribute(T  value, bool rEnable, bool wEnable);

    ~Attribute();

    template<typename T>
    void get(T* value) const;

    template<typename T>
    void set(T value);

    bool isReadingEnable();
    bool isWritingEnable();

private:

    template<typename T>
    void Init(T value, bool rEnable, bool wEnable);

    bool readingEnable;
    bool writingEnable;

    std::any attribute;
};

template<class T>
void Attribute::set(T value)
{
    attribute = value;
}

template<class T>
inline void Attribute::get(T* value) const
{
    *value = std::any_cast<T>(attribute);
}

Что тут не так?...

2
  • Я так и пытался сделать, но получал ошибку при компиляции.
    – Killlin
    28 мая 2020 в 8:08
  • Ну приведите немного более полный код - с примером, когда происходит ошибка при компиляции... Вот, в помощь - ru.stackoverflow.com/help/minimal-reproducible-example
    – Harry
    28 мая 2020 в 8:28

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.