sizeof vs std::is_same comparison

sizeof vs std::is_same comparison

本文关键字:same comparison is std sizeof vs      更新时间:2023-10-16

我目前正在开发一个具有特殊数据结构的简单库。当使用数据时,类将比较请求代理到包含所有操作符重载的泛型代理类。由于某些数据类的长度不同,因此我使用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.

我的问题是,因为它们对我来说都有相同的功能,所以我使用它们会更有效率/更有益。

这两种情况都将被完全优化,并且if块将在编译时要么被写入,要么不被写入。

我会选择更语义的情况(is_same),但取决于你。你看起来很受虐。