С++: Частичная реализация шаблона для указателей
Пытаюсь написать класс с шаблонными методами. Как сделать так, чтобы избавиться от явной реализации и дублированного кода?
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);
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Не соображу, в чем проблема?
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);
}
Что тут не так?...