Skip to content

Condider migrating to std::source_location #186

@stevenwdv

Description

@stevenwdv

Macro's like THROW_IF_WIN32_BOOL_FALSE(b) are painful to use. They have long names, but shorter names could easily clash with other names. I'd much rather do something like using namespace wil::throwing; win32Bool(b). With C++ 20's std::source_location implemented in the latest MSVC version this is now possible.

Additionally, it allows to change the position included in the message. For example:

void throwIfUnexpected(DWORD win32) {
    if (win32 != ERROR_ALREADY_EXISTS) THROW_WIN32(win32);
}

void createFile() {
    if (!CreateFile(... CREATE_ALWAYS ...)) throwIfUnexpected(GetLastError());
}

This will report an error in throwIfUnexpected, but if this function is reused often it would be much more helpful to report the error in the createFile function.

With source_location this would be possible:

void throwIfUnexpected(DWORD win32, std::source_location loc = std::source_location::current()) {
    if (win32 != ERROR_ALREADY_EXISTS) wil::throwWin32(win32, loc); //or something
}

The only downsides are that we cannot get the return address or evaluated expression from source_location. (In C++ 23, the former can be replaced with by using std::stacktrace_entry.)

Very much related: microsoft/cppwinrt#761

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions