microsoft / microsoft/wil

[Feature Request] Result macros for c++23's `std::expected`

Open
#525 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
3k
Forks
300
Avg merge
19h 12m
Merged PRs (30d)
1

Description

Have you considered adding result_macros.h macros for C++23's new std::expected? This would make writing exception-free wrapper code a lot easier.

One discussion to have would be on the Unexpected type that is being returned (i.e. the second template parameter: std::expected<SuccessT, UnexpectedT>). The most straightforward choice would be to directly use HRESULT, BOOL, etc. However, that would loose crucial information when passed around. The THROW_XXX macros use the wil::ResultException for instance, in order to capture the source location, an optional message and other metadata. That's why I'd suggest using it as the unexpected type, too?

Another open question is how to name these macros (e.g. RETURN_UNEXPECTED_XXX, UNEXPECTED_XXX, etc.).

A possible implementation could look something like this:

#define RETURN_UNEXPECTED_IF_FAILED_MSG(hr, fmt, ...) \
    do \
    { \
        const auto __hrRet = wil::verify_hresult(hr); \
        if (FAILED(__hrRet)) \
        { \
           return std::unexpected<wil::ResultException>(std::in_place, __hrRet, _format_msg_(__VA_ARGS__), ...); \
        } \
    } \
    while ((void)0, 0)

Related issues: https://github.com/microsoft/wil/issues/452

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed result_macros.h macros and the related issue #452, then examine how the existing THROW_XXX macros use wil::ResultException. Define the unexpected type and macro naming before implementing support for std::expected; done means exception-free wrapper code can use the new macros with appropriate error metadata.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.