sizeof vs std :: is_same比较

时间:2015-09-21 04:49:37

标签: c++ c++11

我目前正在开发一个具有特殊数据结构的简单库。使用数据时,类将比较请求代理到包含所有运算符重载的通用代理类。由于某些数据类的长度不同,我有if语句检查数据类型是否应该比较该字节位置。

目前我使用sizeof()来比较正在运行的两个数据结构的大小。如果它们大于X,则可以针对字节X - 1计算它们。

示例比较:

if ((sizeof(lhsVT) / sizeof(lhsT) > 2) && (sizeof(rhsVT) / sizeof(rhsT) > 2))
where lhsVT is the lefthand class & lhsT is the lefhand type,
and rhsVT is the righthand class & rhsT is the righthand type.

但是,使用std::is_same将产生相同的比较

if (std::is_same<lhsVT<lhsT>, class<lhsT>>::value &&
    std::is_same<rhsVT<rhsT>, class<rhsT>>::value)
where class is the class it needs to be to continue operation.

我的问题是,因为它们对我来说都具有相同的功能,这对我来说会更有效/更有益。

1 个答案:

答案 0 :(得分:2)

这两种情况都将完全优化,if块将在编译时写入或不写入。

我会选择更具语义性的案例(is_same),但取决于你。你看起来很自虐。