根据模板的类型名称设置私有属性
Set private attribute depending on template's typename
上下文:
我们正在尝试设置一个名为Operand的类模板,该模板可以将几种类型作为类型的T
。这些定义在以下枚举中:
enum eOperandType {
INT8
INT16,
INT32,
FLOAT,
DOUBLE
};
那些对应于<cstdint>
中定义的类型,即int8_t, int16_t
等。
构造函数必须为 Operand(std::string const & value);
。
template<class T>
class Operand : public IOperand
{
public:
Operand(std::string const & value)
{
std::stringstream ss(value);
ss >> _value;
//_type = ??? ;
}
[...]
private:
Operand(void){}
eOperandType _type;
T _value;
};
iOperand的接口在这里没什么重要的,只是某些用于操作员过载的原型。
问题:
设置_type
属性的最佳方法是什么?简单的方法是只用typeid
写几个if/else if
或与之接近的东西,但我觉得那很脏。此外,我只是认为在模板内使用typeid
只是意味着您在某个地方做错了...
使用助手类推断_type
的值。
template <typename T> struct OperandType;
template <> struct OperandType<int8_t>
{
static const eOperandType t = INT8;
};
template <> struct OperandType<int16_t>
{
static const eOperandType t = INT16;
};
等。
并将其用作:
Operand(std::string const & value) : type_(OperandType<T>::t)
{
...
}
ps
鉴于您可以在需要时推导type_
的值,将其存储为会员变量是有意义的吗?
您可以使用模板过载。如果您为每种类型的模板参数进行专门设计,则可以为您设置一个特定参数,以便在专用模板中具有特定值。然后,您可以将该值用于_type
属性。因此,如果您做这样的事情
template<typename _Ty> struct OperandSelector;
template<> struct OperandSelector<int8_t> {
static const eOperandType value = INT8;
}
,然后为您要使用的每个值创建另一个专业化,在您的情况下,INT16
,INT32
,FLOAT
和DOUBLE
。要设置_type
的值,您将用OperandSelector<T>::value
的值分配给它
_type = OperandSelector<T>::value;
这种方法将允许在编译时进行选择,并使选择值简单副本。
template<class...>struct types{using type=types;};
template<class E, E...es>
using enums=std::integer_sequence<E,es...>;
template<class types, class T, class enums>
struct pick;
template<class T0, class...Ts, class T, class E, E e0, E...es>
struct pick< types<T0, Ts...>, T, enums<E,e0,es...>>:
pick< types<Ts...>, T, enums<E,es...>>
{};
template<class T0, class...Ts, class E, E e0, E...es>
struct pick< types<T0, Ts...>, T0, enums<E,e0,es...>>:
std::integral_constant<E,e0>
{};
然后
using c_types = types<int8_t, int16_t, int32_t, float, double>;
using e_types = enums<eOperandType, INT8, INT16, INT32, FLOAT, DOUBLE>;
和班级本身:
static const eOperandType _type = pick<c_types, T, e_types>::value;
这与许多100型类型都无法使用。
在这里我做了static const
,因为它不能变化,所以为什么放任呢?
相关文章:
- C++ 命名参数习惯用语 - 未设置字符串属性
- 如何将字符串属性设置为 QTreeWidgetItem?
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- CMake 设置使用不正确的参数数调用的目标属性
- 如何使用C++获取/设置OBS中的垂直滚动过滤器属性?
- 设置使用 Cereal 序列化库时可以在序列化函数中访问的属性
- 创建新对象并立即为其设置属性时出现编译器错误
- 重载>>运算符未在输入上设置属性
- 清除同一属性后,设置属性时拒绝访问
- 如何在不更改代码的情况下为所有C++函数设置属性
- C++到QML-设置属性
- C++派生类的设置属性到从同一基类派生的未知类对象
- 单词 CAddin - 设置属性与C++
- 从 Lua 设置 C 属性
- 仅在错误的着色器程序中设置属性会影响属性值
- 设置(属性)类的单个/多个成员的最佳做法
- 问题:何时使用继承与设置属性
- Setstylesheet没有单独设置属性
- Linux中的OpenCv videoccapture错误-不支持设置属性#0