Skip to content

<xstring>: resize_and_overwrite generates warning C4018 when Operation returns int #2524

@PowerGamer1

Description

@PowerGamer1

The bug description
According to C++23 draft the Operation op of std::string member function resize_and_overwrite does not have to return size_t, it can also return, for ex., int (https://eel.is/c++draft/string.capacity#8):

[21.3.3.5] Capacity [string.capacity]
...
Mandates: OP has an integer-like type ([iterator.concept.winc]).

When op returns int the compiler produces "warning C4018: '<=': signed/unsigned mismatch" inside STL implementation code that performs _STL_VERIFY check on op return value under debug build.

Command-line test case

#include <string>
int main() {
	std::string s;
	s.resize_and_overwrite(1, [](char* buffer, size_t) {
		*buffer = 'x';
		int i = 1;
		return i;
	});
}

Compile for debug, x86 platform and with /std:c++latest, /W3.

Expected behavior
No warning produced by the compiler.

STL version
STL version from master having resize_and_overwrite implemented.

How to fix
The fix is very simple - replace auto with size_t inside the following line of resize_and_overwrite implementation:

const auto _Result_size = _STD move(_Op)(_Mypair._Myval2._Myptr(), _New_size);

Alternatively, contact compiler team to make them disable useless warning C4018 by default for x86 builds (it is already disabled for x64 builds).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions