Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3598 system_category().default_error_condition(0) #2560

Merged
merged 6 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions stl/inc/system_error
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,12 @@ public:
}

_NODISCARD string message(int _Errcode) const override {
return _Syserror_map(_Errcode);
// TRANSITION, GH-2561, move the logic to syserror.cpp
if (_Errcode == 0) {
return "success";
} else {
return _Syserror_map(_Errcode);
}
}
};

Expand Down Expand Up @@ -544,12 +549,16 @@ public:
}

_NODISCARD error_condition default_error_condition(int _Errval) const noexcept override {
// make error_condition for error code (generic if possible)
const int _Posv = _Winerror_map(_Errval);
if (_Posv == 0) {
return error_condition(_Errval, _STD system_category());
if (_Errval == 0) {
return error_condition(0, _STD generic_category());
} else {
fsb4000 marked this conversation as resolved.
Show resolved Hide resolved
return error_condition(_Posv, _STD generic_category());
// make error_condition for error code (generic if possible)
const int _Posv = _Winerror_map(_Errval);
if (_Posv == 0) {
return error_condition(_Errval, _STD system_category());
} else {
return error_condition(_Posv, _STD generic_category());
}
}
}
};
Expand Down
13 changes: 13 additions & 0 deletions tests/std/tests/Dev11_0493504_error_category_lifetime/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ struct Global {

Global global;

// Also test LWG-3598: system_category().default_error_condition(0) has a generic category.
void test_lwg_3598() {
error_condition cond = system_category().default_error_condition(0);

assert(cond.category() == generic_category());
assert(cond.message() == "success");
assert(cond.value() == 0);

assert(error_code() == error_condition());
fsb4000 marked this conversation as resolved.
Show resolved Hide resolved
}

int main() {
// Also test DevDiv-781294 "<system_error>: Visual C++ 2013 RC system_category().equivalent function does not work".
const error_code code(ERROR_NOT_ENOUGH_MEMORY, system_category());
Expand All @@ -44,6 +55,8 @@ int main() {
// Also test VSO-649432 <system_error> system_category().message() ends with extra newlines
const auto msg = code.message();
assert(!msg.empty() && msg.back() != '\0' && !isspace(static_cast<unsigned char>(msg.back())));

test_lwg_3598();
}


Expand Down
4 changes: 2 additions & 2 deletions tests/tr1/tests/system_error/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void test_main() { // run tests
CHECK(!(ec1 < ec1));

STD error_condition ec0x, ec1x(ERR too_many_links);
CHECK(ec0 != ec0x);
CHECK(ec0 == ec0x);
CHECK(ec0 != ec1x);
CHECK(!STD is_error_code_enum<ERR_ENUM>::value);
CHECK(ec1 == STD make_error_code(ERR too_many_links));
Expand Down Expand Up @@ -167,7 +167,7 @@ void test_main() { // run tests
CHECK(!(ec1 < ec1));

STD error_code ec0x, ec1x = STD make_error_code(ERR too_many_links);
CHECK(ec0 != ec0x);
CHECK(ec0 == ec0x);
CHECK(ec0 != ec1x);
CHECK(STD is_error_condition_enum<ERR_ENUM>::value);
CHECK(ec1 == STD make_error_condition(ERR too_many_links));
Expand Down