检测类型的等价性
detecting equivalence of typedefs
在我的应用程序中,我有一个类型负责(可能)涉及大数的计算,还有一个类型用于处理器之间的通信。
typedef MyBigIntegerClass bigInt;
typedef int smallInt;
通信部分与MyBigIntegerClass不兼容,因此在通信之前,例如bigint的向量必须转换为smallint。到目前为止,没有任何问题。
然而,对于大多数问题实例,使用MyBigIntegerClass是不必要的。事实上,即使是int32_t
也是充分的。这就是为什么我允许像
typedef int32_t bigInt;
typedef int16_t smallInt;
对于计算来说,bigInt类型仍然足够大。这样做的问题是,smallInt必须不同于bigInt。
class Problematic
{
public:
Problematic(bigInt);
Problematic(smallInt);
};
在该类中,构造函数或方法可以接受bigint型或smallint型参数。如果它们相同,则编译失败。
由于代码的其他用户可能想要调整所使用的类型,因此他们可能最终得到像
这样的配置typedef int32_t bigInt;
typedef int32_t smallInt;
和编译以一种不明显的方式失败(至少对一些开发人员来说)。
处理这种情况的一种方法是static_assert(sizeof(bigInt) != sizeof(smallint), "bad config..")
,但我实际上喜欢bigInt == smallInt
的可能性。更改class Problematic
的声明以允许类型等价的好方法是什么?
如果需要保留这两个构造函数,一个可能的解决方案是将int
类型包装在模板中,这意味着它们总是不同的类型,即使底层的int
类型是相同的:
template <typename T>
struct big_int
{
T value;
};
template <typename T>
struct small_int
{
T value;
};
typedef big_int<long> bigInt;
typedef small_int<long> smallInt;
class Problematic
{
public:
Problematic(bigInt) {}
Problematic(smallInt) {}
};
在基础类型相同(http://ideone.com/KGz9Vk)和不相同(http://ideone.com/Pt0XGS)时进行编译。
要允许big_int<>
和small_int<>
作为整型行为,需要实现操作符。例如:
template <typename T>
struct big_int
{
T value;
operator T() { return value; }
big_int& operator+=(big_int const& other)
{
value += other.value;
return *this;
}
template <typename U>
big_int& operator+=(U const& v)
{
value += v;
return *this;
}
big_int& operator++()
{
++value;
return *this;
}
big_int operator++(int)
{
big_int temp = *this;
++value;
return temp;
}
};
这不是详尽的(参见http://herbsutter.com/2013/05/20/gotw-4-class-mechanics/获得关于实现操作符的有用指导)。
- 如何检测 USRP USB 类型?
- 如何检测类型类型别名?
- 用于检测函数类型是否为否的特征
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 如何检测参数包中的uint8_t类型
- SFINAE - 检测类型 T 是指针、数组还是带有随机访问运算符的容器,以及给定的值类型
- 使用SFINAE来检测void返回类型函数的存在
- 如何为QImage检测足够的格式(数据类型)以消耗最少的RAM
- 有没有一种方法可以使用SFINAE来检测一个类型是否实现了给定的抽象基类
- C++模板可以检测特定类型的成员吗
- 检测习惯用语和默认参数类型匹配
- 使用"JSON for Modern C++"库检测整数不适合指定类型?
- SFINAE 检测是否定义了类型
- 检测类型是否为"mapping"
- 检测<T>某些非数字类型 T 的 std::numeric::type 的特化
- 类型检测:使用variadic参数正确实现计算平均值的函数
- 参数类型的编译时类型检测
- C++矩阵乘法类型检测
- 层次结构中的对象类型检测
- 泛型类型检测