admin管理员组文章数量:1203553
The code below
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1,2,3 };
if (std::count(v.begin(), v.end(), 1) > v.size() / 2) {
std::cout << "Too many\n";
}
}
gives a warning:
(8,40) warning C4018: '>': signed/unsigned mismatch
which is quite reasonable, since std::count returns ::difference_type
and size() returns ::size
.
The question is, what is the robust way to handle this?
In the Comparing ptrdiff_t with size_t it is discussed how this should be implemented in compilers, but never said how to safely live with existing implementations.
For example, it is stated:
If
ptrdiff_t
is of greater rank thansize_t
and can represent all positive values ofsize_t
.limit - buf <= sizeof buf
poses no problems then.
Else
ptrdiff_t
may not represent all positive values ofsize_t
and then the subtractionlimit - buf
may be UB per below, so the compare is moot.
Great, but under Win64 we have:
typedef unsigned __int64 size_t;
typedef __int64 ptrdiff_t;
And this is a direct indications (if containers don't use other types for ::difference_type) that size_t
values won't fit to ptrdiff_t
for safe calculations (it can't represent all size_t values as positive).
So, this:
if (std::count(v.begin(), v.end(), 1) > static_cast<std::vector<int>::difference_type>(v.size() / 2)) {
std::cout << "Too many\n";
}
is not safe anymore.
Casting in the opposite way is unacceptable since it violates first guidance above.
So, what is the safest way to shape the code here?
Should I write a wrapper to check the sizes and return the largest type or there is something more reasonable and built-in?
The code below
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1,2,3 };
if (std::count(v.begin(), v.end(), 1) > v.size() / 2) {
std::cout << "Too many\n";
}
}
gives a warning:
(8,40) warning C4018: '>': signed/unsigned mismatch
which is quite reasonable, since std::count returns ::difference_type
and size() returns ::size
.
The question is, what is the robust way to handle this?
In the Comparing ptrdiff_t with size_t it is discussed how this should be implemented in compilers, but never said how to safely live with existing implementations.
For example, it is stated:
If
ptrdiff_t
is of greater rank thansize_t
and can represent all positive values ofsize_t
.limit - buf <= sizeof buf
poses no problems then.
Else
ptrdiff_t
may not represent all positive values ofsize_t
and then the subtractionlimit - buf
may be UB per below, so the compare is moot.
Great, but under Win64 we have:
typedef unsigned __int64 size_t;
typedef __int64 ptrdiff_t;
And this is a direct indications (if containers don't use other types for ::difference_type) that size_t
values won't fit to ptrdiff_t
for safe calculations (it can't represent all size_t values as positive).
So, this:
if (std::count(v.begin(), v.end(), 1) > static_cast<std::vector<int>::difference_type>(v.size() / 2)) {
std::cout << "Too many\n";
}
is not safe anymore.
Casting in the opposite way is unacceptable since it violates first guidance above.
So, what is the safest way to shape the code here?
Should I write a wrapper to check the sizes and return the largest type or there is something more reasonable and built-in?
Share Improve this question edited Jan 21 at 10:00 463035818_is_not_an_ai 122k11 gold badges99 silver badges208 bronze badges asked Jan 20 at 21:04 Damir TenishevDamir Tenishev 3,2727 silver badges20 bronze badges 5 |1 Answer
Reset to default 3The issue is between a signed and unsigned integer value. Vector
does not have an unsigned ssize
function. Instead, use the ssize()
from the standard or ranges library.
auto main() -> int {
std::vector<int> v = { 1,2,3 };
if (std::count(v.begin(), v.end(), 1) > std::ssize(v) / 2) {
std::cout << "Too many\n";
}
return 0;
}
本文标签: cHow to compare sizet and differencetypeStack Overflow
版权声明:本文标题:c++ - How to compare size_t and difference_type? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738666309a2105723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
absl::int128
). – Eljay Commented Jan 21 at 12:33