sizeof vs std::is_same comparison
sizeof vs std::is_same comparison
我目前正在开发一个具有特殊数据结构的简单库。当使用数据时,类将比较请求代理到包含所有操作符重载的泛型代理类。由于某些数据类的长度不同,因此我使用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
),但取决于你。你看起来很受虐。
相关文章:
- 为什么 Same<T, U> 不包含 Same<U, T>?
- Trie*& 和 Trie** 的意思是 Same?
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- C++ "Same Letter"代码无法正常工作
- 在comparison中处理int和std::vector::size_type
- C++ 嵌套类模板 错误 C2440 '=':无法从'type'转换为'same type'
- C++标准在[basic.scope.hiding]中"same scope"到底意味着什么?
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- Boost python getter/setter with the same name
- 使用代码调整删除"comparison is always false"警告
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- 编译 C++ 代码时"Error: comparison between pointer and integer"
- 当 A 和 B "the same"时断言(sizeof(A) == sizeof(B))是否安全?
- 为什么我会看到"member function with the same name as its class must be a constructor"?
- getline repeating same line(c++)(xcode)
- "local variables at the outermost scope of the function may not use the same name as any parameter"是
- Is clang++ ABI same as g++?
- Comparison Boost.Odeint vs Scipy.integrate.odeint?
- 收到错误:"ISO C++ forbids comparison between pointer and integer [-fpermissive]"如何解决?
- PCAP Destination and Source are the same