Skip to content

Commit

Permalink
Fix MSVC C4244 narrowing conversion warning on 64-bit Windows. (#10647)
Browse files Browse the repository at this point in the history
The difference_type (ptrdiff_t) is a narrowing conversion to size_type (int).
MSVC 2019 C++17 issues a C4244 warning about the initialization.

Co-authored-by: Mike Kruskal <62662355+mkruskal-google@users.noreply.github.com>
  • Loading branch information
themoox and mkruskal-google authored Sep 27, 2022
1 parent 86a8760 commit d993377
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/repeated_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ inline void RepeatedField<Element>::Add(Iter begin, Iter end) {
if (std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>::value) {
int additional = std::distance(begin, end);
int additional = static_cast<int>(std::distance(begin, end));
if (additional == 0) return;

int new_size = current_size_ + additional;
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/repeated_ptr_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ inline void RepeatedPtrField<Element>::Add(Iter begin, Iter end) {
if (std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>::value) {
int reserve = std::distance(begin, end);
int reserve = static_cast<int>(std::distance(begin, end));
Reserve(size() + reserve);
}
for (; begin != end; ++begin) {
Expand Down Expand Up @@ -1496,8 +1496,8 @@ RepeatedPtrField<Element>::erase(const_iterator position) {
template <typename Element>
inline typename RepeatedPtrField<Element>::iterator
RepeatedPtrField<Element>::erase(const_iterator first, const_iterator last) {
size_type pos_offset = std::distance(cbegin(), first);
size_type last_offset = std::distance(cbegin(), last);
size_type pos_offset = static_cast<size_type>(std::distance(cbegin(), first));
size_type last_offset = static_cast<size_type>(std::distance(cbegin(), last));
DeleteSubrange(pos_offset, last_offset - pos_offset);
return begin() + pos_offset;
}
Expand Down

0 comments on commit d993377

Please sign in to comment.