diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/ci_tests.yml index 17f36523..11b48fd1 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/ci_tests.yml @@ -29,7 +29,9 @@ jobs: {"preset": "llvm-debug", "image": "ghcr.io/bemanproject/infra-containers-clang:latest"}, {"preset": "llvm-release", "image": "ghcr.io/bemanproject/infra-containers-clang:latest"}, {"preset": "appleclang-debug", "runner": "macos-latest"}, - {"preset": "appleclang-release", "runner": "macos-latest"} + {"preset": "appleclang-release", "runner": "macos-latest"}, + {"preset": "msvc-debug", "runner": "windows-latest"}, + {"preset": "msvc-release", "runner": "windows-latest"} ] build-and-test: @@ -138,6 +140,22 @@ jobs: } ] } + ], + "msvc": [ + { "versions": ["latest"], + "tests": [ + { "cxxversions": ["c++23"], + "tests": [ + { "stdlibs": ["stl"], + "tests": [ + "Debug.Default", "Release.Default", "Release.MaxSan", + "Debug.-DBEMAN_EXPECTED_USE_MODULES=On" + ] + } + ] + } + ] + } ] } diff --git a/CMakePresets.json b/CMakePresets.json index 08a4cb40..89b34361 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -145,7 +145,8 @@ "_debug-base" ], "cacheVariables": { - "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/infra/cmake/msvc-toolchain.cmake" + "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/infra/cmake/msvc-toolchain.cmake", + "CMAKE_CXX_STANDARD": "23" }, "condition": { "type": "equals", @@ -161,7 +162,8 @@ "_release-base" ], "cacheVariables": { - "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/infra/cmake/msvc-toolchain.cmake" + "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/infra/cmake/msvc-toolchain.cmake", + "CMAKE_CXX_STANDARD": "23" }, "condition": { "type": "equals", diff --git a/Makefile b/Makefile index ac3a0e08..4183230a 100755 --- a/Makefile +++ b/Makefile @@ -196,7 +196,12 @@ WORDING_DIR := papers/wording WORDING_FRAGMENTS := \ $(addprefix $(WORDING_DIR)/fragments/,$(addsuffix .tex,$(WORDING_CLAUSES))) -SPECGEN_CLANG_ARGS := -std=c++2c -Iinclude +# The wording shows the standard's expansion of a configuration macro, not +# whichever one the standard library specgen's Clang finds happens to give: +# BEMAN_EXPECTED_CONSTEXPR_EXCEPTION is `constexpr` in the draft, which is +# what this feature-test macro selects. The value is libstdc++ 16's. +SPECGEN_CLANG_ARGS := -std=c++2c -Iinclude \ + -D__cpp_lib_constexpr_exceptions=202502L ifneq ($(SPECGEN_GCC_TOOLCHAIN),) SPECGEN_CLANG_ARGS += --gcc-toolchain=$(SPECGEN_GCC_TOOLCHAIN) endif diff --git a/README.md b/README.md index 069b08cf..fe38e787 100644 --- a/README.md +++ b/README.md @@ -65,18 +65,20 @@ builtin lands in **GCC 13** and **Clang 19**, which sets the floor: * **GCC ≤ 12 / Clang ≤ 17** lack the builtin, so the trait is undefined in C++20 mode. -* **Clang 18** has the builtin but rejects out-of-line definitions of constrained - members whose `requires`-clause names the enclosing `expected<…>` type (or a - member typedef) with a spelling that differs from the in-class declaration — - GCC and Clang 19+ canonicalize these, Clang 18 does not. +* **Clang 18** has the builtin. It rejects an out-of-line definition of a + constrained member whose `requires`-clause names the enclosing `expected<…>` + type (or a member typedef) with a spelling that differs from the in-class + declaration — GCC and Clang 19+ canonicalize these, Clang 18 does not. Each + such definition is therefore spelled exactly as its declaration, and Clang 18 + accepts the header; it is not in the CI matrix. **MSVC is not currently supported.** In C++23 the trait is available, but MSVC applies the same out-of-line-definition constraint-matching strictness as Clang 18 (`C2244: unable to match function definition to an existing declaration`). -The fix is to make each such member's definition token-identical to its -declaration; that work is not done here simply because we do not have direct -MSVC access to develop and verify the workarounds. Contributions with MSVC -access are welcome. +The four definitions MSVC 19.52 rejected for this are now token-identical to +their declarations, checked with Clang 18, which rejected the same four. We do +not have direct MSVC access, so the MSVC jobs in CI, which build and test at +C++23, are the only verification. Contributions with MSVC access are welcome. The Windows setup below is retained for contributors who want to investigate MSVC support. It is currently unsupported and unverified; the documented build diff --git a/docs/plan/specgen-catchup.md b/docs/plan/specgen-catchup.md index 0de815a6..e8f5f834 100644 --- a/docs/plan/specgen-catchup.md +++ b/docs/plan/specgen-catchup.md @@ -25,7 +25,13 @@ done as far as it can go. What is left of it waits on [reference-error-overloads](#reference-error-overloads) and on specgen, and [validation-findings](#validation-findings) and [exposition-helpers](#exposition-helpers) wait with it. -[macro-specifiers](#macro-specifiers) waits on specgen#377. +On the branch `specgen-0.58` (from `upstream/main` at `54bd02e`, 2026-11-30): +the wording regenerated with specgen 0.58.0, and +[macro-specifiers](#macro-specifiers) and +[implementation-extras](#implementation-extras) done, now that every +specgen request this plan waited on is closed, and +[derived-let](#derived-let) carried out. What else that unblocks is +in the [specgen-pin](#specgen-pin) Log entry for the day. --- @@ -505,6 +511,13 @@ prevents: that is stage waits for it. Check: the macro appears 0 times in the wording, and `[expected.bad.void]` is left with the `= default` marks alone. +Done on `specgen-0.58` (2026-11-30), with specgen 0.58.0: `SPECGEN_CLANG_ARGS` +defines `__cpp_lib_constexpr_exceptions=202502L`, as +[macro-expansion-args](#macro-expansion-args) recommends, and the header is +unchanged. The macro appears 0 times; against the draft at n5054, +`[expected.bad.void]` is 8 / 0, the `= default`s, and `[expected.bad]` 1 / 0, +the parameter name. + ### Stage 8 — [member-routing](#member-routing) Replace the banner comments with `// \ref{stable.name}, title` group @@ -585,6 +598,14 @@ Not done, and why: `bad_expected_access` match the draft's, which waits on specgen#377 and specgen#385. +2026-11-30, with specgen 0.58.0: the deduction guide is in `unexpected`'s +synopsis (specgen#384) and "Assignment rebinds" opens `[expected.ref.assign]` +(specgen#383), with no change to the headers. specgen#385 covers the +`noexcept` and `= default` cases, done under +[implementation-extras](#implementation-extras); a parameter name in the +synopsis has no marker yet. `[expected.bad.void]`'s synopsis is now the +draft's, so its copy in [draft-text-copies](#draft-text-copies) can go. + ### Stage 10 — [class-prose](#class-prose) Rewrite the general subclauses as the draft has them: unlabelled paragraphs @@ -901,6 +922,10 @@ draft diff is the check that the class agrees with the standard. - 2026-11-19: the one in `expected.hpp` is gone, replaced by the source of `[expected.syn]` (`ad9ca81`). The two in `bad_expected_access.hpp` stay until the generated synopses there match the draft's. +- 2026-11-30: `[expected.bad.void]` has no marks against n5054, so its copy + is gone, on `specgen-0.58`, with Steve Downey's agreement. The + `[expected.bad]` copy stays until the synopsis drops the constructor's + parameter name, which no marker does yet. ### clause-order @@ -967,13 +992,21 @@ spelling, of the kind specgen#282 settled for `= delete`. constructors and copy assignment operators of the three `expected` classes, which the library declares `noexcept(...)` and the draft does not. Nothing is changed in the headers until specgen has a marker. +- 2026-11-30: specgen#385 gave `\omit noexcept` and `\omit default`. On + `specgen-0.58`, `\omit noexcept` on `unexpected`'s three constructors, + `expected`'s default constructor, and the copy constructors and copy + assignment operators of `expected` and `expected`; + `\omit default` on `bad_expected_access`'s six special members. + `[expected.bad.void]` has no marks against n5054, and + `[expected.unexpected]` is 10 / 3, from 16 / 7. The parameter name has + no marker; it is the third case of specgen#385, which closed without it. ### macro-expansion-args **Question.** Which expansion of a configuration macro does the wording show, and where does the build say so? -**Status.** OPEN -**Recommendation.** The expansion the standard has: `constexpr`, by adding +**Status.** DECIDED +**Decision.** The expansion the standard has: `constexpr`, by adding `-D__cpp_lib_constexpr_exceptions=202502L` to `SPECGEN_CLANG_ARGS`. **Why.** specgen writes what the macro expands to in the translation unit it parses, so the wording follows whichever standard library and compiler @@ -983,9 +1016,15 @@ it. Stating the macro on the command line makes the render the same on every machine, and the value is the one libstdc++ 16 defines. The cost is that the wording run and a real build may then parse the header differently, which is what the option is for. +**Decided by.** Steve Downey, 2026-11-30. **Log.** - 2026-11-19: opened. With the define and the `#undef` removed, on scratch, the macro appears 0 times and `constexpr` stands in its place. +- 2026-11-30: specgen#377 reads the `#undef`ed macro, so the define alone + does it. Carried out on `specgen-0.58` for + [macro-specifiers](#macro-specifiers), as recommended; still OPEN for the + ruling. +- 2026-11-30: decided as recommended. ### unchanged-wording-source @@ -1163,6 +1202,159 @@ headers by design, deferring to the primary template, and that belongs to **Log.** - 2026-11-19: opened, in carrying out [synopsis-shape](#synopsis-shape). +### derived-let + +**Question.** The monadic operations name a type `U` or `G`. Now that +specgen derives the draft's leading "Let `U` be ..." from the body's +alias, how do the docblocks and the alias divide the work? +**Status.** DECIDED +**Decision.** The alias is spelled as the draft binds it, with +`decltype((val_))` and `decltype(std::move(val_))` (`error()` for `G`), and +the derived paragraph is the binding. Where the draft's spelling names a +different type from the code's, the code's stays: +[rvalue-error-binding](#rvalue-error-binding). The authored *Mandates* name `U` in the +draft's words, and the trailing "where `U` is ..." in *Effects* goes. +**Why.** The 21 derived paragraphs say each binding a second time beside the +docblock's own. And the alias is read from one overload: for the `&` and +`const&` pair it says `invoke_result_t`, which is wrong for `const&`. +`decltype((val_))` is the same type in each body and is the draft's text. +Using `U` in *Mandates* also lets `--validate` match the authored +`is_same_v` conjunct with the derived one. +**Decided by.** Steve Downey, 2026-11-30: take the recommendation. +**Log.** +- 2026-11-30: opened. Tried on the lvalue `and_then` of the primary template + on `specgen-0.58`: the item reads as `[expected.object.monadic]` does but + for the equivalent-to code block, one note fewer, and the tests pass. The + other 20 are left. In `[expected.ref.monadic]` the paper is the only text, + and `val_` is a `T*` there, so the binding is `decltype(*val_)` or `T&`, + which is the same type for every overload. +- 2026-11-30: decided, and carried out on `specgen-0.58` for the 18 + derived bindings left: the monadic operations of all three classes and + `value_or` of `expected`. 19 were derived; the other two of the + 21 counted are authored "Let `r`" sentences. `U` in + `expected` stays `T&`. `transform` and `transform_error` called + the result type `U` and the error type `G2`, against the derived + binding; they now use the draft's names, with an authored "Let" for + `transform`, whose assertions are not leading. 105 notes, from 110. + Carrying it out found that the recommendation's `error()` is wrong for + the rvalue overloads when `E` is a reference, so those twelve aliases, + four in each class, keep + `E&&`: [rvalue-error-binding](#rvalue-error-binding). + +### rvalue-error-binding + +**Question.** The rvalue overloads of `or_else` and `transform_error` bind +`G` from the error as an rvalue. When `E` is a reference, what type is +that? +**Status.** DECIDED +**Decision.** The type of `std::move(unex).error()`: the held +`unexpected` is moved and then asked for the error. That is `E&&` for an +object `E` and `E&` for a reference `E`. The same expression replaces +`std::move(error())` wherever the wording has an rvalue error, not only in +the binding of `G`. Not `std::move(*this).error()`, which has the same type +but has an object declare itself expiring. +**Why.** For `E` = `X&`, `error() &&` returns `E&&`, which collapses to `X&`, +and the body passes `std::move(unex_).error()`, which is `X&` too, so the +alias `E&&` is exact. The draft's spelling, `decltype(std::move(error()))`, +is `X&&`: written into the alias it would compute a different `G`, and a +callable taking `X&` would stop compiling. So the derived paragraph says +`invoke_result_t`, which is right for `&&` but reads `E&&` for the +`const&&` overload it also covers. The same spelling in the authored +*Constraints* of the rvalue `and_then` and `transform`, +`is_constructible_v`, misstates the +code's `is_constructible_v` for a reference `E`. Both belong with +[reference-error-overloads](#reference-error-overloads): the paper's +wording does not yet say what a reference `E` does here. A spelling that +is exact for both overloads is `decltype(std::move(*this).error())`. +**Decided by.** Steve Downey, 2026-11-30. +**Log.** +- 2026-11-30: opened, from [derived-let](#derived-let). +- 2026-11-30: reviewed, at Steve Downey's prompting that this is the + `boost::optional` steal. It is, and the paper already rules on it: + D4280's section on shallow conversions makes move-the-wrapper-then-access + a guarantee and names `std::move(r).error()` as the form. The code + follows it everywhere: no `std::move` is applied to an error or value + access outside a `decltype` in a non-reference branch. The wording does + not: `std::move(error())` is in 26 places in the docblocks, 23 in + `expected-new.tex`. For a reference `E` they are, by use: 3 `error_or` + *Returns* and 3 `value()` *Throws*, where the library itself would + initialize an object from the referent as an rvalue; 7 in the rvalue + `or_else` and `transform_error`, which hand the callable an rvalue; 6 + `and_then` and `transform` *Effects*, `U(unexpect, std::move(error()))`, + ill-formed for any reference `E`, by constructibility for `X&` and by + the deleted `unexpect_t` overload for `const X&`; and 7 *Constraints* and + *Mandates*, false for `X&`, where the code keeps the overload, and true + for `const X&`. A scratch program checked each against the code with a + type that records being moved from: nothing is, in all three classes. + The tests assert that for the conversions only; the rvalue `error_or`, + `value()` and monadic operations on a reference `E` have no such test. + The same-type move constructor and assignment also write + `std::move(rhs.error())`, 3 places, and are covered by the rebinding + paragraph. The working draft made the same repair to `optional`: its + converting constructor from `optional&&` reads + `std::move(rhs).operator*()`. Still OPEN: the spelling is + `std::move(*this).error()` or `std::move(unex).error()`, which the + docblock of `error() &&` already uses. +- 2026-11-30: decided, and carried out on `specgen-0.58`. `unex` is in + scope wherever it is needed: all three classes declare + `unexpected unex`, in the headers and in `expected-new.tex`, and the + draft names an exposition-only member inside `decltype` in the same + positions, as `decltype(std::move(val))`. In the code the twelve aliases + and the two assertions of `value() &&` are in function bodies. The one + place `unex_` cannot be named is a leading requires-clause, and those + keep `E&&`, which is exact there because each overload has its own. 26 + places in the docblocks; 22 marked and 1 unmarked in `expected-new.tex`, + whose marks in `[expected.expected]` go from 9 / 2 to 20 / 13 and in + `[expected.void]` from 7 / 0 to 18 / 11. Tests added for the guarantee; + 1258 pass. 105 notes, unchanged. Left: the same-type move constructor + and assignment still read `std::move(rhs.error())`, under the rebinding + paragraph; and D4280's prose does not yet say why its wording marks + these 22 places. +- 2026-11-30: D4280 now says why. + Its section on shallow conversions gained a closing paragraph naming the draft's `std::move(error())` as access-then-move and `std::move(unex).error()` as the replacement. + The section is retitled "An rvalue `expected` must not let its referent be stolen", since it now covers the rvalue observers and monadic operations as well as conversions. + Left: the same-type move constructor and assignment, as above. +- 2026-11-30: a 23rd place marked in `expected-new.tex`, the *Mandates* of `value() &&` in `[expected.object.obs]`. + A `\linebreak{}` inside `std::move` had hidden it from the search. + The rebinding paragraph covers the move assignment only, not the two move constructors, which are in `[expected.object.cons]` and `[expected.void.cons]` with no such paragraph. + For a reference `E` their `std::move(rhs.error())` initializes an `unexpected` from an rvalue, which `[expected.un.ref]` rejects. + +### reference-result-of-transform + +**Question.** `transform` and `transform_error` compute the result type with `remove_cv_t`, which leaves a reference a reference. +When the callable returns an lvalue reference, is the call ill-formed, or does it yield an `expected` that refers to the result? +**Status.** DECIDED +**Decision.** It yields a reference `expected`: `transform` returns `expected` and `transform_error` returns `expected`, on all three class templates. +An rvalue reference result stays ill-formed. +**Why.** `optional` is the precedent. +P2988R12 widened "valid contained type" to admit lvalue references, and `optional::transform` *Mandates* that definition, so a callable returning `U&` yields `optional`. +Before this the two operations disagreed with each other and with the wording. +The paper kept the draft's definition of "valid value type", which excludes references, so its `transform` rejected a reference result and, read literally, so did `expected` itself, while the code returned `expected`. +The paper's relaxed rule for `unexpected` already admitted a reference `G` in `transform_error`, while the code rejected it and four compile-fail tests held it there. +**Decided by.** Steve Downey, 2026-11-30: "4 and 5 are decided by the Optional precedent, at least in spirit." + +**Log.** +- 2026-11-30: opened and decided, from a check of the base specializations against the small changes P2988R12 and its follow-up issues made to `optional` outside `[optional.ref]`. + Carried out on `specgen-0.58`. + The definition of "valid value type" in `[expected.object.general]` gains lvalue references to non-array object types, and the four *Returns* of `transform` say what `val` is for a reference `U`. + The code's `transform_error` assertions admit an lvalue reference `G`. + The four compile-fail tests now return an rvalue reference, which is still rejected, and are renamed `*_rvalue_ref_result_fail`. + Left: the `transform` docblocks say `expected(in_place, ...)`, which the reference specialization deletes; the code and the paper do not. + Left: the hazard `optional` also has, a callable that returns a reference into the value or error of an rvalue `expected`, is not diagnosed. +- 2026-11-30: the same check found, and this branch fixes, in the paper's wording: the move constructors' `std::move(rhs.error())`, now `std::move(rhs.unex)` (see [rvalue-error-binding](#rvalue-error-binding)); the *Constraints* of `swap`, which tested `is_swappable_v` on a reference; and the *Mandates* of `value()`, which tested `is_copy_constructible_v` on a reference and were missing from `[expected.ref.obs]`. + In the code: the deleted constructor templates of `expected` and `unexpected` deduced the class itself from a const rvalue and made its copy a hard error under gcc-16. + Left for a ruling: the *Remarks* that delete those constructors have the same hole in the paper; `[expected.syn]` declares neither `expected` nor `unexpected`; and `expected` has no assertion that a reference `E` refers to an object type. + +- 2026-11-30: the items left above are done, at Steve Downey's direction. + The *Remarks* that delete the reference constructors now read "This constructor is defined as deleted if ...", after *Constraints* that no longer mention the temporary, as `optional`'s do; this covers the `in_place_t` constructor of `unexpected` too, which the code deletes and the paper had only constrained. + `[expected.syn]` declares `unexpected` and `expected`. + The six `transform` docblocks have the draft's shape, with *Mandates*, and no longer name the `in_place_t` constructor. + `expected` and `expected` assert that `E`, less any reference, is an object type, as `expected` and `unexpected` already did; nothing that compiled before is rejected, since `unexpected` already refused a function type, and the paper's `[expected.un.ref]` now says so. + D4280's "Monadic operations are uniform" states the reference result and the dangling hazard, which is left to the caller as it is for `optional`. + Left: `[expected.ref.general]` does not mark `value()` freestanding-deleted, though the synopsis now calls the specialization partially freestanding. +- 2026-11-30: `value()` is marked freestanding-deleted in `[expected.ref.general]` and in the two docblocks, which closes the item above. + D4280 gains a short section, "A referent is a non-array object type": `optional` admits array and function referents and LWG4304 keeps that working, and the paper says it does not rewrite `expected` to follow without explicit direction. + ### specgen-pin **Question.** How is the specgen version fixed and recorded? @@ -1181,6 +1373,25 @@ the availability test in the makefile, and the pin are revisited then. status changed: a specgen from 0.44.0 on fails `make wording` on the headers on `main`, where an older one succeeds. The cost of having no pin is now a build that works or not by which specgen is on `PATH`. +- 2026-11-30: 0.58.0 (`0d034bc`) installed from the release archive. The + checked-in wording on `upstream/main` (`54bd02e`) no longer matched any + current specgen's render, so `make wording-check` failed there; nothing + runs it in CI. Regenerated (branch `specgen-0.58`). `--validate` gives + the same 107 notes as 0.56.2. What moved: code wraps at the draft's 97 + columns (specgen#446), and a synopsis puts a short template head on its + declaration's line, as the draft sometimes does; the macro, deduction + guide and section-prose fixes above; and 21 derived "Let" paragraphs + (specgen#442), the subject of [derived-let](#derived-let). Two defects + found, each with a scratch reproduction, not yet filed: a qualified + exposition-only name, `typename expected::error_value_type`, in a + derived *Constraints* is shown as source, worked around with an authored + `\constraints` on the primary's two `error_or`; and code is wrapped by its + width before a specifier macro is removed, so + `operator=(const bad_expected_access&) noexcept;` breaks at 80 columns + rendered. +- 2026-11-30: both filed, as + [specgen#487](https://github.com/steve-downey/specgen/issues/487) and + [specgen#488](https://github.com/steve-downey/specgen/issues/488). ## Requests to specgen @@ -1228,6 +1439,14 @@ Filed 2026-11-19, from [synopsis-shape](#synopsis-shape): | [specgen#386](https://github.com/steve-downey/specgen/issues/386) | An empty `\constraints` removes the derived element, which the guide does not say; and a *Remarks* cannot cover a derived conjunct. | [overload-partitions](#overload-partitions), [validation-findings](#validation-findings) | | [specgen#392](https://github.com/steve-downey/specgen/issues/392) | With `--baseline-draft`, a `// \ref{...}` comment in a `\verbatim-synopsis` block is marked removed and added again where the draft has the same bytes. | [front-matter](#front-matter), [generated-diff](#generated-diff) | +Filed 2026-11-30, from the 0.58.0 round, each with a scratch-header +reproduction: + +| Issue | What | For | +|---|---|---| +| [specgen#487](https://github.com/steve-downey/specgen/issues/487) | A derived *Constraints* shows a qualified exposition-only name, `typename box::value_kind`, as source, and the drift check does not match it with the unqualified authored spelling. | the primary template's `error_or` | +| [specgen#488](https://github.com/steve-downey/specgen/issues/488) | Code is wrapped by its width before a specifier macro is removed, so it breaks, and aligns its continuation, by the macro's width. | `[expected.bad.void]` | + Not filed: a set of Doxygen `ALIASES` for specgen's markers, until [api-site-markup](#api-site-markup) has one that works here. diff --git a/include/beman/expected/bad_expected_access.hpp b/include/beman/expected/bad_expected_access.hpp index 716416da..6770b4e4 100644 --- a/include/beman/expected/bad_expected_access.hpp +++ b/include/beman/expected/bad_expected_access.hpp @@ -35,25 +35,6 @@ namespace std { } */ -/*** -22.8.5 Class template specialization bad_expected_access[expected.bad.void] -namespace std { - template<> - class bad_expected_access : public exception { - protected: - constexpr bad_expected_access() noexcept; - constexpr bad_expected_access(const bad_expected_access&) noexcept; - constexpr bad_expected_access(bad_expected_access&&) noexcept; - constexpr bad_expected_access& operator=(const bad_expected_access&) noexcept; - constexpr bad_expected_access& operator=(bad_expected_access&&) noexcept; - constexpr ~bad_expected_access(); - - public: - constexpr const char* what() const noexcept override; - }; -} - */ - namespace beman { namespace expected { @@ -64,12 +45,18 @@ class bad_expected_access; template <> class bad_expected_access : public std::exception { protected: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access() noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access(const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access(bad_expected_access&&) noexcept = default; + //! \omit default + BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access() noexcept = default; + //! \omit default + BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access(const bad_expected_access&) noexcept = default; + //! \omit default + BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access(bad_expected_access&&) noexcept = default; + //! \omit default BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=(const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=(bad_expected_access&&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION ~bad_expected_access() = default; + //! \omit default + BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=(bad_expected_access&&) noexcept = default; + //! \omit default + BEMAN_EXPECTED_CONSTEXPR_EXCEPTION ~bad_expected_access() = default; public: BEMAN_EXPECTED_CONSTEXPR_EXCEPTION const char* what() const noexcept override; diff --git a/include/beman/expected/expected.hpp b/include/beman/expected/expected.hpp index d23cac73..94cb04c9 100644 --- a/include/beman/expected/expected.hpp +++ b/include/beman/expected/expected.hpp @@ -38,6 +38,9 @@ //! // \ref{expected.unexpected}, class template \tcode{unexpected} //! template class unexpected; //! +//! // \ref{expected.un.ref}, partial specialization of \tcode{unexpected} for lvalue reference types +//! template class unexpected; +//! //! // \ref{expected.bad}, class template \tcode{bad_expected_access} //! template class bad_expected_access; //! @@ -55,6 +58,9 @@ //! //! // \ref{expected.void}, partial specialization of \tcode{expected} for \tcode{void} types //! template requires is_void_v class expected; // partially freestanding +//! +//! // \ref{expected.ref}, partial specialization of \tcode{expected} for lvalue reference types +//! template class expected; // partially freestanding //! } /// END [expected.syn] @@ -142,12 +148,14 @@ constexpr bool converts_from_any_cvref = std::disjunction_v` is `void` or a complete non-array object type that is not -//! `in_place_t`, `unexpect_t`, or a specialization of `unexpected`. A program -//! which instantiates class template `expected` with an argument `T` -//! that is not a valid value type for `expected` is ill-formed. A program that -//! instantiates the definition of the template `expected` with a type -//! for the `E` parameter that is not a valid template argument for -//! `unexpected` is ill-formed. +//! `in_place_t`, `unexpect_t`, or a specialization of `unexpected`, or if `T` +//! is an lvalue reference to an object type that is not an array +//! type\iref{expected.ref.general}. A program which instantiates class +//! template `expected` with an argument `T` that is not a valid value +//! type for `expected` is ill-formed. A program that instantiates the +//! definition of the template `expected` with a type for the `E` +//! parameter that is not a valid template argument for `unexpected` is +//! ill-formed. //! \covers mandates !is_reference_v //! \covers mandates !is_rvalue_reference_v //! \covers mandates !is_void_v> @@ -156,6 +164,7 @@ constexpr bool converts_from_any_cvref = std::disjunction_v //! \covers mandates !is-unexpected-specialization>::value //! \covers mandates !is_array_v> +//! \covers mandates is_object_v> //! //! When `T` is not \cv{} `void`, it shall meet the \oldconcept{Destructible} //! requirements (\tref{cpp17.destructible}). If `E` is not an lvalue reference @@ -171,6 +180,7 @@ class expected { static_assert(!detail::is_unexpected_specialization>::value, "T must not be a specialization of unexpected"); static_assert(!std::is_array_v>, "E must not be an array type"); + static_assert(std::is_object_v>, "E must be an object type"); private: //! \expos @@ -255,7 +265,7 @@ class expected { template > requires(!std::is_same_v, std::in_place_t> && !std::is_same_v, unexpect_t> && - !std::is_same_v, expected> && std::is_constructible_v && + !std::is_same_v, expected> && std::is_constructible_v && !detail::is_unexpected_specialization>::value && (!std::is_same_v> || !detail::is_expected_specialization>::value)) @@ -429,7 +439,7 @@ class expected { // Assignment from value U&& template > - requires(!std::is_same_v> && + requires(!std::is_same_v, std::remove_cvref_t> && !detail::is_unexpected_specialization>::value && std::is_constructible_v && std::is_assignable_v && (std::is_nothrow_constructible_v || std::is_nothrow_move_constructible_v || @@ -536,11 +546,13 @@ class expected { constexpr T value_or(U&& def) &&; template - requires(std::is_copy_constructible_v && std::is_convertible_v) + requires(std::is_copy_constructible_v>> && + std::is_convertible_v>>) constexpr error_value_type error_or(G&& def) const&; template - requires(std::is_move_constructible_v && std::is_convertible_v) + requires(std::is_move_constructible_v>> && + std::is_convertible_v>>) constexpr error_value_type error_or(G&& def) &&; // \ref{expected.object.monadic}, monadic operations @@ -647,6 +659,7 @@ class expected { // \rSec3[expected.object.cons]{Constructors} +//! \omit noexcept //! \effects Value-initializes `val`. //! \ensures `has_value()` is `true`. //! \throws Any exception thrown by the initialization of `val`. @@ -657,6 +670,7 @@ constexpr expected::expected() noexcept(std::is_nothrow_default_constructi std::construct_at(std::addressof(val_)); } +//! \omit noexcept //! \constraints //! \effects If `rhs.has_value()` is `true`, direct-non-list-initializes //! `val` with `*rhs`. Otherwise, direct-non-list-initializes `unex` with @@ -687,7 +701,7 @@ constexpr expected::expected(const expected& rhs) noexcept(std::is_nothrow //! `true`. //! \effects If `rhs.has_value()` is `true`, direct-non-list-initializes //! `val` with `std::move(*rhs)`. Otherwise, direct-non-list-initializes -//! `unex` with `std::move(rhs.error())`. +//! `unex` with `std::move(rhs.unex)`. //! \ensures `rhs.has_value()` is unchanged; `rhs.has_value() == //! this->has_value()` is `true`. //! \throws Any exception thrown by the initialization of `val` or `unex`. @@ -931,6 +945,7 @@ constexpr expected::~expected() // ============================================================================= // \rSec3[expected.object.assign]{Assignment} +//! \omit noexcept //! \constraints //! \effects If `this->has_value() && rhs.has_value()`, equivalent to `val //! = *rhs`. Otherwise, if `this->has_value()`, equivalent to @@ -993,7 +1008,7 @@ constexpr expected& expected::operator=(const expected& rhs) noexcep //! is_nothrow_move_constructible_v` is `true`. //! \effects If `this->has_value() && rhs.has_value()`, equivalent to `val //! = std::move(*rhs)`. Otherwise, if `this->has_value()`, equivalent to -//! `reinit-expected(unex, val, std::move(rhs.error()))`. Otherwise, if +//! `reinit-expected(unex, val, std::move(rhs.unex))`. Otherwise, if //! `rhs.has_value()`, equivalent to `reinit-expected(val, unex, //! std::move(*rhs))`. Otherwise, equivalent to `unex = //! std::move(rhs.unex)`. Then, if no exception was thrown, equivalent to: @@ -1346,7 +1361,7 @@ constexpr bool expected::has_value() const noexcept { //! \freestanding-deleted //! \group obs-value-lval -//! \mandates `is_copy_constructible_v` is `true`. +//! \mandates `is_copy_constructible_v` is `true`. //! \returns `val`, if `has_value()` is `true`. //! \throws `bad_expected_access(as_const(error()))` if `has_value()` is //! `false`. @@ -1370,10 +1385,11 @@ constexpr T& expected::value() & { //! \freestanding-deleted //! \group obs-value-rval -//! \mandates `is_copy_constructible_v` is `true` and -//! `is_constructible_v` is `true`. +//! \mandates `is_copy_constructible_v` is `true` and +//! `is_constructible_v` is `true`. //! \returns `std::move(val)`, if `has_value()` is `true`. -//! \throws `bad_expected_access(std::move(error()))` if `has_value()` is +//! \throws `bad_expected_access(std::move(unex).error())` if `has_value()` is //! `false`. template constexpr const T&& expected::value() const&& { @@ -1381,7 +1397,8 @@ constexpr const T&& expected::value() const&& { static_assert(std::is_copy_constructible_v && std::is_move_constructible_v, "value() const&& requires E to be copy and move constructible"); } else { - static_assert(std::is_copy_constructible_v && std::is_constructible_v, + static_assert(std::is_copy_constructible_v && + std::is_constructible_v, "value() && requires E be copy-constructible and constructible from move(error())"); } if (!has_val_) @@ -1397,7 +1414,8 @@ constexpr T&& expected::value() && { static_assert(std::is_copy_constructible_v && std::is_move_constructible_v, "value() && requires E to be copy and move constructible"); } else { - static_assert(std::is_copy_constructible_v && std::is_constructible_v, + static_assert(std::is_copy_constructible_v && + std::is_constructible_v, "value() && requires E be copy-constructible and constructible from move(error())"); } if (!has_val_) @@ -1472,28 +1490,32 @@ constexpr T expected::value_or(U&& def) && { return static_cast(std::forward(def)); } +//! \constraints `is_copy_constructible_v` is `true` and +//! `is_convertible_v` is `true`. //! \mandates `is_copy_constructible_v` is `true` and //! `is_convertible_v` is `true`. //! \returns `std::forward(def)` if `has_value()` is `true`, `error()` //! otherwise. template template - requires(std::is_copy_constructible_v::error_value_type> && - std::is_convertible_v::error_value_type>) + requires(std::is_copy_constructible_v>> && + std::is_convertible_v>>) constexpr typename expected::error_value_type expected::error_or(G&& def) const& { if (!has_val_) return unex_.error(); return static_cast(std::forward(def)); } +//! \constraints `is_move_constructible_v` is `true` and +//! `is_convertible_v` is `true`. //! \mandates `is_move_constructible_v` is `true` and //! `is_convertible_v` is `true`. //! \returns `std::forward(def)` if `has_value()` is `true`, -//! `std::move(error())` otherwise. +//! `std::move(unex).error()` otherwise. template template - requires(std::is_move_constructible_v::error_value_type> && - std::is_convertible_v::error_value_type>) + requires(std::is_move_constructible_v>> && + std::is_convertible_v>>) constexpr typename expected::error_value_type expected::error_or(G&& def) && { if (!has_val_) return std::move(unex_).error(); @@ -1505,17 +1527,15 @@ constexpr typename expected::error_value_type expected::error_or(G&& //! \group monadic-and-then-lval //! \constraints `is_constructible_v` is `true`. -//! \mandates `remove_cvref_t>` is a -//! specialization of `expected` and its `error_type` is the same type as -//! `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return //! invoke(std::forward(f), val); else return U(unexpect, error());` -//! where `U` is `remove_cvref_t>`. template template requires std::is_constructible_v constexpr auto expected::and_then(F&& f) & { - using U = std::remove_cvref_t>; + using U = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "and_then: F must return a specialization of expected"); static_assert(std::is_same_v, @@ -1526,20 +1546,18 @@ constexpr auto expected::and_then(F&& f) & { } //! \group monadic-and-then-rval -//! \constraints `is_constructible_v` is +//! \constraints `is_constructible_v` is //! `true`. -//! \mandates `remove_cvref_t>` -//! is a specialization of `expected` and its `error_type` is the same -//! type as `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return //! invoke(std::forward(f), std::move(val)); else return U(unexpect, -//! std::move(error()));` where `U` is `remove_cvref_t>`. +//! std::move(unex).error());` template template requires std::is_constructible_v constexpr auto expected::and_then(F&& f) && { - using U = std::remove_cvref_t>; + using U = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "and_then: F must return a specialization of expected"); static_assert(std::is_same_v, @@ -1554,7 +1572,7 @@ template template requires std::is_constructible_v constexpr auto expected::and_then(F&& f) const& { - using U = std::remove_cvref_t>; + using U = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "and_then: F must return a specialization of expected"); static_assert(std::is_same_v, @@ -1569,7 +1587,7 @@ template template requires std::is_constructible_v constexpr auto expected::and_then(F&& f) const&& { - using U = std::remove_cvref_t>; + using U = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "and_then: F must return a specialization of expected"); static_assert(std::is_same_v, @@ -1581,16 +1599,14 @@ constexpr auto expected::and_then(F&& f) const&& { //! \group monadic-or-else-lval //! \constraints `is_constructible_v` is `true`. -//! \mandates `remove_cvref_t>` is -//! `expected` for some type `X`. +//! \mandates `G` is `expected` for some type `X`. //! \effects Equivalent to: `if (has_value()) return G(in_place, val); else -//! return invoke(std::forward(f), error());` where `G` is -//! `remove_cvref_t>`. +//! return invoke(std::forward(f), error());` template template requires std::is_constructible_v constexpr auto expected::or_else(F&& f) & { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -1602,17 +1618,15 @@ constexpr auto expected::or_else(F&& f) & { //! \group monadic-or-else-rval //! \constraints `is_constructible_v` is //! `true`. -//! \mandates `remove_cvref_t>` is `expected` for some type `X`. +//! \mandates `G` is `expected` for some type `X`. //! \effects Equivalent to: `if (has_value()) return G(in_place, //! std::move(val)); else return invoke(std::forward(f), -//! std::move(error()));` where `G` is `remove_cvref_t>`. +//! std::move(unex).error());` template template requires std::is_constructible_v constexpr auto expected::or_else(F&& f) && { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -1626,7 +1640,7 @@ template template requires std::is_constructible_v constexpr auto expected::or_else(F&& f) const& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -1640,7 +1654,7 @@ template template requires std::is_constructible_v constexpr auto expected::or_else(F&& f) const&& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -1650,17 +1664,23 @@ constexpr auto expected::or_else(F&& f) const&& { } //! \group monadic-transform-lval +//! Let `U` be `remove_cv_t>`. //! \constraints `is_constructible_v` is `true`. -//! \effects Equivalent to: `if (!has_value()) return U(unexpect, -//! error()); else return expected(in_place, -//! invoke(std::forward(f), val));` where `U2` is -//! `remove_cv_t>` and `U` is -//! `expected`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f), val));` is +//! well-formed. +//! \effects If `has_value()` is `false`, returns `expected(unexpect, +//! error())`. Otherwise, if `is_void_v` is `false`, returns an `expected` object whose `has_val` member is `true` and `val` member is +//! direct-non-list-initialized with `invoke(std::forward(f), val)`; if `U` +//! is an lvalue reference type, `val` instead points to the object to which +//! the result of that invocation refers. Otherwise, evaluates +//! `invoke(std::forward(f), val)` and then returns `expected()`. template template requires std::is_constructible_v constexpr auto expected::transform(F&& f) & { - using U = std::remove_cv_t>; + using U = std::remove_cv_t>; if constexpr (!std::is_void_v) { static_assert(!std::is_array_v, "transform: U must not be an array type"); static_assert(!std::is_same_v, std::in_place_t>, "transform: U must not be in_place_t"); @@ -1682,18 +1702,25 @@ constexpr auto expected::transform(F&& f) & { } //! \group monadic-transform-rval -//! \constraints `is_constructible_v` is +//! Let `U` be `remove_cv_t>`. +//! \constraints `is_constructible_v` is //! `true`. -//! \effects Equivalent to: `if (!has_value()) return -//! U(unexpect, std::move(error())); else return expected(in_place, invoke(std::forward(f), std::move(val)));` where `U2` -//! is `remove_cv_t>` and `U` -//! is `expected`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f), std::move(val)));` +//! is well-formed. +//! \effects If `has_value()` is `false`, returns `expected(unexpect, +//! std::move(unex).error())`. Otherwise, if `is_void_v` is `false`, returns +//! an `expected` object whose `has_val` member is `true` and `val` +//! member is direct-non-list-initialized with `invoke(std::forward(f), +//! std::move(val))`; if `U` is an lvalue reference type, `val` instead points +//! to the object to which the result of that invocation refers. Otherwise, +//! evaluates `invoke(std::forward(f), std::move(val))` and then returns +//! `expected()`. template template requires std::is_constructible_v constexpr auto expected::transform(F&& f) && { - using U = std::remove_cv_t>; + using U = std::remove_cv_t>; if constexpr (!std::is_void_v) { static_assert(!std::is_array_v, "transform: U must not be an array type"); static_assert(!std::is_same_v, std::in_place_t>, "transform: U must not be in_place_t"); @@ -1719,7 +1746,7 @@ template template requires std::is_constructible_v constexpr auto expected::transform(F&& f) const& { - using U = std::remove_cv_t>; + using U = std::remove_cv_t>; if constexpr (!std::is_void_v) { static_assert(!std::is_array_v, "transform: U must not be an array type"); static_assert(!std::is_same_v, std::in_place_t>, "transform: U must not be in_place_t"); @@ -1745,7 +1772,7 @@ template template requires std::is_constructible_v constexpr auto expected::transform(F&& f) const&& { - using U = std::remove_cv_t>; + using U = std::remove_cv_t>; if constexpr (!std::is_void_v) { static_assert(!std::is_array_v, "transform: U must not be an array type"); static_assert(!std::is_same_v, std::in_place_t>, "transform: U must not be in_place_t"); @@ -1768,19 +1795,19 @@ constexpr auto expected::transform(F&& f) const&& { //! \group monadic-transform-error-lval //! \constraints `is_constructible_v` is `true`. -//! \effects Equivalent to: `if (has_value()) return G(in_place, val); else -//! return expected(unexpect, invoke(std::forward(f), -//! error()));` where `G2` is `remove_cv_t>` and `G` is `expected`. +//! \effects Equivalent to: `if (has_value()) return expected(in_place, val); else return expected(unexpect, +//! invoke(std::forward(f), error()));` template template requires std::is_constructible_v constexpr auto expected::transform_error(F&& f) & { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(std::in_place, val_); @@ -1790,20 +1817,19 @@ constexpr auto expected::transform_error(F&& f) & { //! \group monadic-transform-error-rval //! \constraints `is_constructible_v` is //! `true`. -//! \effects Equivalent to: `if (has_value()) return G(in_place, -//! std::move(val)); else return expected(unexpect, -//! invoke(std::forward(f), std::move(error())));` where `G2` is -//! `remove_cv_t>` and -//! `G` is `expected`. +//! \effects Equivalent to: `if (has_value()) return expected(in_place, std::move(val)); else return expected(unexpect, +//! invoke(std::forward(f), std::move(unex).error()));` template template requires std::is_constructible_v constexpr auto expected::transform_error(F&& f) && { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(std::in_place, std::move(val_)); @@ -1815,11 +1841,12 @@ template template requires std::is_constructible_v constexpr auto expected::transform_error(F&& f) const& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(std::in_place, val_); @@ -1831,11 +1858,12 @@ template template requires std::is_constructible_v constexpr auto expected::transform_error(F&& f) const&& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(std::in_place, std::move(val_)); @@ -1867,6 +1895,7 @@ constexpr auto expected::transform_error(F&& f) const&& { //! \covers mandates !is_rvalue_reference_v //! \covers mandates !is_void_v> //! \covers mandates !is_array_v> +//! \covers mandates is_object_v> //! \covers mandates is_reference_v || is_same_v, E> //! \covers mandates !is-unexpected-specialization>>::value //! @@ -1877,6 +1906,7 @@ class expected { static_assert(!std::is_rvalue_reference_v, "E must not be an rvalue reference"); static_assert(!std::is_void_v>, "E must not be void"); static_assert(!std::is_array_v>, "E must not be an array type"); + static_assert(std::is_object_v>, "E must be an object type"); static_assert(std::is_reference_v || std::is_same_v, E>, "E must not be cv-qualified"); static_assert(!detail::is_unexpected_specialization>>::value, "E must not be an unexpected specialization"); @@ -2275,6 +2305,7 @@ constexpr expected::expected() noexcept : has_val_(true), void_() {} template constexpr expected::expected(std::in_place_t) noexcept : has_val_(true), void_() {} +//! \omit noexcept //! \constraints //! \effects If `rhs.has_value()` is `false`, direct-non-list-initializes //! `unex` with `rhs.error()`. @@ -2295,7 +2326,7 @@ constexpr expected::expected(const expected& rhs) noexcept(std::is_noth //! \constraints `is_move_constructible_v` is `true` or //! `is_reference_v` is `true`. //! \effects If `rhs.has_value()` is `false`, direct-non-list-initializes -//! `unex` with `std::move(rhs.error())`. +//! `unex` with `std::move(rhs.unex)`. //! \ensures `rhs.has_value()` is unchanged; `rhs.has_value() == //! this->has_value()` is `true`. //! \throws Any exception thrown by the initialization of `unex`. @@ -2466,6 +2497,7 @@ constexpr expected::~expected() // ============================================================================= // \rSec3[expected.void.assign]{Assignment} +//! \omit noexcept //! \constraints //! \effects If `this->has_value() && rhs.has_value()`, no effects. //! Otherwise, if `this->has_value()`, equivalent to: @@ -2681,7 +2713,7 @@ constexpr void expected::operator*() const noexcept { } //! \freestanding-deleted -//! \mandates `is_copy_constructible_v` is `true`. +//! \mandates `is_copy_constructible_v` is `true`. //! \throws `bad_expected_access(error())` if `has_value()` is `false`. template constexpr void expected::value() const& { @@ -2691,9 +2723,9 @@ constexpr void expected::value() const& { } //! \freestanding-deleted -//! \mandates `is_copy_constructible_v` is `true` and -//! `is_move_constructible_v` is `true`. -//! \throws `bad_expected_access(std::move(error()))` if `has_value()` is +//! \mandates `is_copy_constructible_v` is `true` and +//! `is_move_constructible_v` is `true`. +//! \throws `bad_expected_access(std::move(unex).error())` if `has_value()` is //! `false`. template constexpr void expected::value() && { @@ -2764,7 +2796,7 @@ constexpr typename expected::error_value_type expected::error_ //! \mandates `is_move_constructible_v` is `true` and //! `is_convertible_v` is `true`. //! \returns `std::forward(def)` if `has_value()` is `true`, -//! `std::move(error())` otherwise. +//! `std::move(unex).error()` otherwise. template template requires(std::is_move_constructible_v>> && @@ -2779,11 +2811,10 @@ constexpr typename expected::error_value_type expected::error_ //! \group void-monadic-and-then-lval //! \constraints `is_constructible_v` is `true`. -//! \mandates `remove_cvref_t>` is a specialization of -//! `expected` and its `error_type` is the same type as `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return -//! invoke(std::forward(f)); else return U(unexpect, error());` where -//! `U` is `remove_cvref_t>`. +//! invoke(std::forward(f)); else return U(unexpect, error());` template template requires std::is_constructible_v @@ -2799,13 +2830,13 @@ constexpr auto expected::and_then(F&& f) & { } //! \group void-monadic-and-then-rval -//! \constraints `is_constructible_v` is +//! \constraints `is_constructible_v` is //! `true`. -//! \mandates `remove_cvref_t>` is a specialization of -//! `expected` and its `error_type` is the same type as `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return //! invoke(std::forward(f)); else return U(unexpect, -//! std::move(error()));` where `U` is `remove_cvref_t>`. +//! std::move(unex).error());` template template requires std::is_constructible_v @@ -2851,16 +2882,14 @@ constexpr auto expected::and_then(F&& f) const&& { } //! \group void-monadic-or-else-lval -//! \mandates `remove_cvref_t>` is a -//! specialization of `expected` and its `value_type` is the same type as -//! `T`. +//! \mandates `G` is a specialization of `expected` and its `value_type` is +//! the same type as `T`. //! \effects Equivalent to: `if (has_value()) return G(); else return -//! invoke(std::forward(f), error());` where `G` is -//! `remove_cvref_t>`. +//! invoke(std::forward(f), error());` template template constexpr auto expected::or_else(F&& f) & { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v, "or_else: F must return expected with the same value_type"); @@ -2870,16 +2899,14 @@ constexpr auto expected::or_else(F&& f) & { } //! \group void-monadic-or-else-rval -//! \mandates `remove_cvref_t>` is a specialization of `expected` and -//! its `value_type` is the same type as `T`. +//! \mandates `G` is a specialization of `expected` and its `value_type` is +//! the same type as `T`. //! \effects Equivalent to: `if (has_value()) return G(); else return -//! invoke(std::forward(f), std::move(error()));` where `G` is -//! `remove_cvref_t>`. +//! invoke(std::forward(f), std::move(unex).error());` template template constexpr auto expected::or_else(F&& f) && { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v, "or_else: F must return expected with the same value_type"); @@ -2892,7 +2919,7 @@ constexpr auto expected::or_else(F&& f) && { template template constexpr auto expected::or_else(F&& f) const& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v, "or_else: F must return expected with the same value_type"); @@ -2905,7 +2932,7 @@ constexpr auto expected::or_else(F&& f) const& { template template constexpr auto expected::or_else(F&& f) const&& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v, "or_else: F must return expected with the same value_type"); @@ -2915,15 +2942,17 @@ constexpr auto expected::or_else(F&& f) const&& { } //! \group void-monadic-transform-lval +//! Let `U` be `remove_cv_t>`. //! \constraints `is_constructible_v` is `true`. -//! \mandates `U` is a valid value type for `expected`, where `U` is -//! `remove_cv_t>`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f)));` is well-formed. //! \effects If `has_value()` is `false`, returns `expected(unexpect, -//! error())`. Otherwise, if `is_void_v` is `false`, returns an -//! `expected` object whose `has_val` member is `true` and `val` -//! member is direct-non-list-initialized with `invoke(std::forward(f))`. -//! Otherwise, evaluates `invoke(std::forward(f))` and then returns -//! `expected()`. +//! error())`. Otherwise, if `is_void_v` is `false`, returns an `expected` object whose `has_val` member is `true` and `val` member is +//! direct-non-list-initialized with `invoke(std::forward(f))`; if `U` is an +//! lvalue reference type, `val` instead points to the object to which the +//! result of that invocation refers. Otherwise, evaluates +//! `invoke(std::forward(f))` and then returns `expected()`. template template requires std::is_constructible_v @@ -2950,16 +2979,18 @@ constexpr auto expected::transform(F&& f) & { } //! \group void-monadic-transform-rval -//! \constraints `is_constructible_v` is +//! Let `U` be `remove_cv_t>`. +//! \constraints `is_constructible_v` is //! `true`. -//! \mandates `U` is a valid value type for `expected`, where `U` is -//! `remove_cv_t>`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f)));` is well-formed. //! \effects If `has_value()` is `false`, returns `expected(unexpect, -//! std::move(error()))`. Otherwise, if `is_void_v` is `false`, returns +//! std::move(unex).error())`. Otherwise, if `is_void_v` is `false`, returns //! an `expected` object whose `has_val` member is `true` and `val` -//! member is direct-non-list-initialized with `invoke(std::forward(f))`. -//! Otherwise, evaluates `invoke(std::forward(f))` and then returns -//! `expected()`. +//! member is direct-non-list-initialized with `invoke(std::forward(f))`; if +//! `U` is an lvalue reference type, `val` instead points to the object to +//! which the result of that invocation refers. Otherwise, evaluates +//! `invoke(std::forward(f))` and then returns `expected()`. template template requires std::is_constructible_v @@ -3040,8 +3071,7 @@ constexpr auto expected::transform(F&& f) const&& { //! \group void-monadic-transform-error-lval //! \mandates `G` is a valid template argument for `unexpected` and the //! declaration `G g(invoke(std::forward(f), error()));` is -//! well-formed, where `G` is `remove_cv_t>`. +//! well-formed. //! \returns If `has_value()` is `true`, `expected()`; otherwise, an //! `expected` object whose `has_val` member is `false` and `unex` //! member is direct-non-list-initialized with `invoke(std::forward(f), @@ -3049,11 +3079,12 @@ constexpr auto expected::transform(F&& f) const&& { template template constexpr auto expected::transform_error(F&& f) & { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(); @@ -3062,21 +3093,21 @@ constexpr auto expected::transform_error(F&& f) & { //! \group void-monadic-transform-error-rval //! \mandates `G` is a valid template argument for `unexpected` and the -//! declaration `G g(invoke(std::forward(f), std::move(error())));` is -//! well-formed, where `G` is `remove_cv_t>`. +//! declaration `G g(invoke(std::forward(f), std::move(unex).error()));` is +//! well-formed. //! \returns If `has_value()` is `true`, `expected()`; otherwise, an //! `expected` object whose `has_val` member is `false` and `unex` //! member is direct-non-list-initialized with `invoke(std::forward(f), -//! std::move(error()))`. +//! std::move(unex).error())`. template template constexpr auto expected::transform_error(F&& f) && { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(); @@ -3087,11 +3118,12 @@ constexpr auto expected::transform_error(F&& f) && { template template constexpr auto expected::transform_error(F&& f) const& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(); @@ -3102,11 +3134,12 @@ constexpr auto expected::transform_error(F&& f) const& { template template constexpr auto expected::transform_error(F&& f) const&& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(); @@ -3214,12 +3247,13 @@ class expected { // Value constructor — takes U that can bind to T& //! \seebelow explicit //! \constraints `remove_cvref_t` is not `in_place_t`, `expected`, or - //! a specialization of `unexpected`; `is_constructible_v` is - //! `true`; and `reference_constructs_from_temporary_v` is - //! `false`. + //! a specialization of `unexpected`; and `is_constructible_v` is + //! `true`. //! \effects Let `r` be the lvalue result of `T& r = //! std::forward(u);`. Initializes `val` with `addressof(r)`. //! \ensures `has_value()` is `true`. + //! \remarks This constructor is defined as deleted if + //! `reference_constructs_from_temporary_v` is `true`. template requires(!std::is_same_v, std::in_place_t> && !std::is_same_v, expected> && @@ -3231,12 +3265,15 @@ class expected { val_ = std::addressof(r); } - // Deleted: binding a temporary to T& creates a dangling reference - //! \remarks A constructor for which - //! `reference_constructs_from_temporary_v` is `true` — one that - //! would bind `T&` to a temporary — is defined as deleted. + // Deleted: binding a temporary to T& creates a dangling reference. + // Represented in wording by the enabled overload above, whose \remarks + // says when it is deleted. + //! \merge template - requires(detail::reference_constructs_from_temporary_v) + requires(!std::is_same_v, std::in_place_t> && + !std::is_same_v, expected> && + !detail::is_unexpected_specialization>::value && + detail::reference_constructs_from_temporary_v) constexpr expected(U&&) = BEMAN_EXPECTED_DELETE_MSG( "expected: argument would bind a temporary that dangles; pass an lvalue reference"); @@ -4043,6 +4080,8 @@ constexpr bool expected::has_value() const noexcept { return has_val_; } +//! \freestanding-deleted +//! \mandates `is_copy_constructible_v` is `true`. //! \returns `*val` if `has_value()` is `true`. //! \throws `bad_expected_access(as_const(error()))` if `has_value()` is //! `false`. @@ -4054,8 +4093,12 @@ constexpr T& expected::value() const& { return *val_; } +//! \freestanding-deleted +//! \mandates `is_copy_constructible_v` is `true` and +//! `is_constructible_v` is `true`. //! \returns `*val` if `has_value()` is `true`. -//! \throws `bad_expected_access(std::move(error()))` if `has_value()` is +//! \throws `bad_expected_access(std::move(unex).error())` if `has_value()` is //! `false`. template constexpr T& expected::value() && { @@ -4114,11 +4157,11 @@ constexpr E&& expected::error() && noexcept { return std::move(unex_).error(); } -//! \mandates `is_convertible_v>` and -//! `is_convertible_v>` are `true`. -//! \returns `has_value() ? static_cast>(*val) : -//! static_cast>(std::forward(def))`. The result is an -//! object, never a reference. +//! \mandates `is_convertible_v` and `is_convertible_v` are +//! `true`. +//! \returns `has_value() ? static_cast(*val) : +//! static_cast(std::forward(def))`. The result is an object, never a +//! reference. template template requires(std::is_object_v && !std::is_array_v) @@ -4144,7 +4187,7 @@ constexpr typename expected::error_value_type expected::error_or(G } //! \returns `std::forward(def)` if `has_value()` is `true`, -//! `std::move(error())` otherwise. The result is an object, never a +//! `std::move(unex).error()` otherwise. The result is an object, never a //! reference. template template @@ -4160,11 +4203,10 @@ constexpr typename expected::error_value_type expected::error_or(G //! \group ref-monadic-and-then-lval //! \constraints `is_constructible_v` is `true`. -//! \mandates `remove_cvref_t>` is a specialization -//! of `expected` and its `error_type` is the same type as `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return //! invoke(std::forward(f), *val); else return U(unexpect, error());` -//! where `U` is `remove_cvref_t>`. //! \remarks The member templates `and_then`, `or_else`, `transform`, and //! `transform_error` behave as specified for the primary template, with //! one difference: the value is passed to the callable as `T&` for every @@ -4186,14 +4228,13 @@ constexpr auto expected::and_then(F&& f) & { } //! \group ref-monadic-and-then-rval -//! \constraints `is_constructible_v` is +//! \constraints `is_constructible_v` is //! `true`. -//! \mandates `remove_cvref_t>` is a specialization -//! of `expected` and its `error_type` is the same type as `E`. +//! \mandates `U` is a specialization of `expected` and +//! `is_same_v` is `true`. //! \effects Equivalent to: `if (has_value()) return //! invoke(std::forward(f), *val); else return U(unexpect, -//! std::move(error()));` where `U` is `remove_cvref_t>`. +//! std::move(unex).error());` template template requires std::is_constructible_v @@ -4239,15 +4280,13 @@ constexpr auto expected::and_then(F&& f) const&& { } //! \group ref-monadic-or-else-lval -//! \mandates `remove_cvref_t>` is -//! `expected` for some type `X`. +//! \mandates `G` is `expected` for some type `X`. //! \effects Equivalent to: `if (has_value()) return G(*val); else return -//! invoke(std::forward(f), error());` where `G` is -//! `remove_cvref_t>`. +//! invoke(std::forward(f), error());` template template constexpr auto expected::or_else(F&& f) & { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -4257,15 +4296,13 @@ constexpr auto expected::or_else(F&& f) & { } //! \group ref-monadic-or-else-rval -//! \mandates `remove_cvref_t>` is `expected` for some type `X`. +//! \mandates `G` is `expected` for some type `X`. //! \effects Equivalent to: `if (has_value()) return G(*val); else return -//! invoke(std::forward(f), std::move(error()));` where `G` is -//! `remove_cvref_t>`. +//! invoke(std::forward(f), std::move(unex).error());` template template constexpr auto expected::or_else(F&& f) && { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -4278,7 +4315,7 @@ constexpr auto expected::or_else(F&& f) && { template template constexpr auto expected::or_else(F&& f) const& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -4291,7 +4328,7 @@ constexpr auto expected::or_else(F&& f) const& { template template constexpr auto expected::or_else(F&& f) const&& { - using G = std::remove_cvref_t>; + using G = std::remove_cvref_t>; static_assert(detail::is_expected_specialization::value, "or_else: F must return a specialization of expected"); static_assert(std::is_same_v>, "or_else: F must return expected for some X"); @@ -4301,11 +4338,18 @@ constexpr auto expected::or_else(F&& f) const&& { } //! \group ref-monadic-transform-lval +//! Let `U` be `remove_cv_t>`. //! \constraints `is_constructible_v` is `true`. -//! \effects Equivalent to: `if (!has_value()) return U(unexpect, -//! error()); else return expected(in_place, -//! invoke(std::forward(f), *val));` where `U2` is -//! `remove_cv_t>` and `U` is `expected`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f), *val));` is +//! well-formed. +//! \effects If `has_value()` is `false`, returns `expected(unexpect, +//! error())`. Otherwise, if `is_void_v` is `false`, returns an `expected` object whose `has_val` member is `true` and `val` member is +//! direct-non-list-initialized with `invoke(std::forward(f), *val)`; if `U` +//! is an lvalue reference type, `val` instead points to the object to which +//! the result of that invocation refers. Otherwise, evaluates +//! `invoke(std::forward(f), *val)` and then returns `expected()`. template template requires std::is_constructible_v @@ -4332,12 +4376,19 @@ constexpr auto expected::transform(F&& f) & { } //! \group ref-monadic-transform-rval -//! \constraints `is_constructible_v` is +//! Let `U` be `remove_cv_t>`. +//! \constraints `is_constructible_v` is //! `true`. -//! \effects Equivalent to: `if (!has_value()) return U(unexpect, -//! std::move(error())); else return expected(in_place, -//! invoke(std::forward(f), *val));` where `U2` is -//! `remove_cv_t>` and `U` is `expected`. +//! \mandates `U` is a valid value type for `expected`. If `is_void_v` is +//! `false`, the declaration `U u(invoke(std::forward(f), *val));` is +//! well-formed. +//! \effects If `has_value()` is `false`, returns `expected(unexpect, +//! std::move(unex).error())`. Otherwise, if `is_void_v` is `false`, returns +//! an `expected` object whose `has_val` member is `true` and `val` +//! member is direct-non-list-initialized with `invoke(std::forward(f), +//! *val)`; if `U` is an lvalue reference type, `val` instead points to the +//! object to which the result of that invocation refers. Otherwise, evaluates +//! `invoke(std::forward(f), *val)` and then returns `expected()`. template template requires std::is_constructible_v @@ -4416,18 +4467,18 @@ constexpr auto expected::transform(F&& f) const&& { } //! \group ref-monadic-transform-error-lval -//! \effects Equivalent to: `if (has_value()) return G(*val); else return -//! expected(unexpect, invoke(std::forward(f), error()));` -//! where `G2` is `remove_cv_t>` and -//! `G` is `expected`. +//! \effects Equivalent to: `if (has_value()) return expected(*val); +//! else return expected(unexpect, invoke(std::forward(f), +//! error()));` template template constexpr auto expected::transform_error(F&& f) & { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(*val_); @@ -4435,19 +4486,18 @@ constexpr auto expected::transform_error(F&& f) & { } //! \group ref-monadic-transform-error-rval -//! \effects Equivalent to: `if (has_value()) return G(*val); else return -//! expected(unexpect, invoke(std::forward(f), -//! std::move(error())));` where `G2` is -//! `remove_cv_t>` and -//! `G` is `expected`. +//! \effects Equivalent to: `if (has_value()) return expected(*val); +//! else return expected(unexpect, invoke(std::forward(f), +//! std::move(unex).error()));` template template constexpr auto expected::transform_error(F&& f) && { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(*val_); @@ -4458,11 +4508,12 @@ constexpr auto expected::transform_error(F&& f) && { template template constexpr auto expected::transform_error(F&& f) const& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(*val_); @@ -4473,11 +4524,12 @@ constexpr auto expected::transform_error(F&& f) const& { template template constexpr auto expected::transform_error(F&& f) const&& { - using G = std::remove_cv_t>; - static_assert(std::is_object_v, "transform_error: G must be an object type"); - static_assert(!std::is_array_v, "transform_error: G must not be an array type"); + using G = std::remove_cv_t>; + static_assert(std::is_object_v> && !std::is_rvalue_reference_v, + "transform_error: G must be an object type or an lvalue reference to one"); + static_assert(!std::is_array_v>, "transform_error: G must not be an array type"); static_assert(std::is_same_v>, "transform_error: G must not be cv-qualified"); - static_assert(!detail::is_unexpected_specialization::value, + static_assert(!detail::is_unexpected_specialization>::value, "transform_error: G must not be a specialization of unexpected"); if (has_val_) return expected(*val_); diff --git a/include/beman/expected/unexpected.hpp b/include/beman/expected/unexpected.hpp index cc5d6af2..c4b78e6b 100644 --- a/include/beman/expected/unexpected.hpp +++ b/include/beman/expected/unexpected.hpp @@ -85,6 +85,7 @@ class unexpected { constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; + //! \omit noexcept //! \constraints //! \item `is_same_v, unexpected>` is `false`; and //! \item `is_same_v, in_place_t>` is `false`; and @@ -98,6 +99,7 @@ class unexpected { constexpr explicit unexpected(Err&& e) noexcept(std::is_nothrow_constructible_v) : unex_(std::forward(e)) {} + //! \omit noexcept //! \constraints `is_constructible_v` is `true`. //! \effects Direct-non-list-initializes `unex` with //! `std::forward(args)...`. @@ -108,6 +110,7 @@ class unexpected { Args&&... args) noexcept(std::is_nothrow_constructible_v) : unex_(std::forward(args)...) {} + //! \omit noexcept //! \constraints `is_constructible_v&, Args...>` is //! `true`. //! \effects Direct-non-list-initializes `unex` with `il, @@ -207,14 +210,12 @@ class unexpected { //! \at expected.un.ref //! \constraints //! \item `is_same_v, unexpected>` is `false`, - //! \item `is_same_v, in_place_t>` is `false`, - //! \item `is_constructible_v` is `true`, and - //! \item `reference_constructs_from_temporary_v` is `false`. + //! \item `is_same_v, in_place_t>` is `false`, and + //! \item `is_constructible_v` is `true`. //! \effects Initializes `unex` with //! `addressof(static_cast(std::forward(e)))`. - //! \remarks A constructor for which - //! `reference_constructs_from_temporary_v` is `true` — one that - //! would bind `E&` to a temporary — is defined as deleted. + //! \remarks This constructor is defined as deleted if + //! `reference_constructs_from_temporary_v` is `true`. template requires(!std::is_same_v, unexpected> && !std::is_same_v, std::in_place_t> && std::is_constructible_v && @@ -225,7 +226,9 @@ class unexpected { // enabled overload above, whose \remarks says when it's deleted instead. //! \merge template - requires(detail::reference_constructs_from_temporary_v) + requires(!std::is_same_v, unexpected> && + !std::is_same_v, std::in_place_t> && + detail::reference_constructs_from_temporary_v) constexpr unexpected(G&&) = BEMAN_EXPECTED_DELETE_MSG( "unexpected: argument would bind a temporary that dangles; pass an lvalue reference"); @@ -244,10 +247,11 @@ class unexpected { // reference or not. Naturally restricted to arity 1: there is no variadic overload here, // and expected only ever calls this when is_constructible_v already holds. //! \at expected.un.ref - //! \constraints `is_constructible_v` is `true` and - //! `reference_constructs_from_temporary_v` is `false`. + //! \constraints `is_constructible_v` is `true`. //! \effects Initializes `unex` with //! `addressof(static_cast(std::forward(e)))`. + //! \remarks This constructor is defined as deleted if + //! `reference_constructs_from_temporary_v` is `true`. template requires(std::is_constructible_v && !detail::reference_constructs_from_temporary_v) constexpr explicit unexpected(std::in_place_t, G&& e) noexcept diff --git a/papers/D4280R0.tex b/papers/D4280R0.tex index e242cefb..234bf824 100644 --- a/papers/D4280R0.tex +++ b/papers/D4280R0.tex @@ -441,7 +441,7 @@ \chapter{Design choices and decisions} The implementation was built against a numbered decision log. The decisions that shape the interface are argued here, so LEWG can poll against the numbers. -\section{Rebind on assignment, never assign-through (D1)} +\section{Rebind on assignment, never assign-through} Assignment to an engaged \tcode{expected} rebinds the internal pointer. It does not assign through to the referent. @@ -459,7 +459,7 @@ \section{Rebind on assignment, never assign-through (D1)} \tcode{emplace} rebinds too --- it is the only thing it can do --- and \tcode{swap} exchanges the two pointers and leaves the referents alone. -\section{Shallow \tcode{const} (D2)} +\section{Shallow \tcode{const}} A \tcode{const expected} yields a non-\tcode{const} \tcode{T\&} from \tcode{operator*}. The binding is \tcode{const}; the referent is not. The model @@ -472,7 +472,7 @@ \section{Shallow \tcode{const} (D2)} promise --- a \tcode{T* const} does not either --- and the mistake is in reading \tcode{const} on a handle as \tcode{const} on the referent. -\section{Dangling constructors are deleted, with a diagnostic (D3)} +\section{Dangling constructors are deleted, with a diagnostic} Every constructor that would bind \tcode{T\&} (or \tcode{E\&}) to a temporary is deleted, guarded by \tcode{reference_constructs_from_temporary_v} @@ -493,7 +493,7 @@ \section{Dangling constructors are deleted, with a diagnostic (D3)} rejecting prvalues would therefore require information this interface cannot recover, so both are conservatively rejected. -\section{References in the error position, by relaxation not specialization (D4)} +\section{References in the error position, by relaxation not specialization} The error type is a template parameter, and errors are routinely owned elsewhere --- a diagnostics engine, an error registry, a long-lived context @@ -507,7 +507,7 @@ \section{References in the error position, by relaxation not specialization (D4) to express \tcode{expected} while \tcode{expected} works would be inconsistent, hostile, and strictly worse than disallowing both. -\section{\tcode{unexpected}, and the reference-only rule (D5)} +\section{\tcode{unexpected}, and the reference-only rule} To construct an \tcode{expected} with a reference error, \tcode{unexpected} has to be able to hold one, so \tcode{unexpected} is part of the proposal. It @@ -563,7 +563,7 @@ \section{\tcode{value\_type} is \tcode{T}, not \tcode{T\&}} \tcode{expected} for the reference specialization. No program that was valid before is rejected. -\section{\tcode{value\_or} returns by value; \tcode{error\_or} mirrors it (D6)} +\section{\tcode{value\_or} returns by value; \tcode{error\_or} mirrors it} \tcode{value_or} returns \tcode{T} by value, as \tcode{optional::value_or} does \cite{P2988R12}: a reference return would @@ -578,7 +578,15 @@ \section{\tcode{value\_or} returns by value; \tcode{error\_or} mirrors it (D6)} the reason instead of leaving the reader to infer it from ``no member named \tcode{value_or}.'' -\section{No default constructor, no in-place value, \tcode{emplace} rebinds (D7)} +\section{A referent is a non-array object type} + +\tcode{optional} admits a referent that is an array or a function, and \cite{LWG4304} adjusted its \tcode{value_or} so that such an \tcode{optional} is well-formed even though it cannot return its referent by value. +This paper does not rewrite \tcode{expected} to pick that up. +The referent of \tcode{expected}, and of a reference \tcode{E}, is an object type that is not an array, in line with what \tcode{expected} already requires of a value \tcode{T}. +Admitting the rest would mean reworking \tcode{value_or} and \tcode{error_or}, which return by value, and \tcode{value()}, whose exception carries a copy of the error. +That is not taken on here without explicit direction to do so. + +\section{No default constructor, no in-place value, \tcode{emplace} rebinds} An engaged \tcode{expected} must hold a real reference; there is no null reference, so there is no default constructor. \tcode{expected(in_place, ...)} @@ -586,7 +594,7 @@ \section{No default constructor, no in-place value, \tcode{emplace} rebinds (D7) rebinds the pointer, which is the only thing it can do; nothing is being ``emplaced,'' but the name is kept for parallelism with the primary template. -\section{Monadic operations are uniform (D8)} +\section{Monadic operations are uniform} \tcode{and_then}, \tcode{transform}, \tcode{or_else}, and \tcode{transform_error} are provided on every specialization. On a reference @@ -597,13 +605,21 @@ \section{Monadic operations are uniform (D8)} kept for parity with the primary template even though, for a reference, they do the same work. -\section{Shallow conversions must not steal (D9)} +A callable that returns an lvalue reference gives a reference result: \tcode{transform} returns \tcode{expected} and \tcode{transform_error} returns \tcode{expected}, each referring to the object the callable returned. +This follows \tcode{optional}, whose \tcode{transform} returns \tcode{optional} in the same case \cite{P2988R12}. +A result that is an rvalue reference is ill-formed. + +Such a result can dangle. +A callable can return a reference into the value or the error of an rvalue \tcode{expected}, or to anything else that does not outlive the result. +That is a property of the callable, and neither \tcode{expected} nor \tcode{optional} can detect it. +The same question was debated for \tcode{optional}, and the conclusion is the same here: the possibility of misuse does not warrant withholding the capability. -Converting a reference-holding \tcode{expected} to a value-holding one copies -the referent; it never moves out of it, even from an rvalue source. -\tcode{std::move} on an \tcode{expected} moves the handle --- a pointer ---- not the object it points at. This is the \tcode{boost::optional} -pitfall, stated as a guarantee callers may rely on: +\section{An rvalue \tcode{expected} must not let its referent be stolen} + +Converting a reference-holding \tcode{expected}, whether the reference is in the value or the error position, to a value-holding one copies the referent; it never moves out of it, even from an rvalue source. +Calling \tcode{std::move} on such an \tcode{expected} moves the handle --- a pointer --- not the object it points at. +Moving out of the referent is the \tcode{boost::optional} pitfall. +Here the copy is guaranteed: \begin{minted}[fontsize=\small]{c++} std::string s = "bar"; @@ -611,13 +627,16 @@ \section{Shallow conversions must not steal (D9)} std::expected v{std::move(r)}; // copies; s == "bar" afterwards \end{minted} -The internal rule that secures this is move-the-wrapper-then-access. For -example, use \tcode{*std::move(r)} or \tcode{std::move(r).error()}. Never use -access-then-move: \tcode{std::move(*r)} would force an rvalue onto a reference -the wrapper does not own, and steal from the caller's object. Because it is a -guarantee and not merely an implementation habit, it belongs in the design. +The internal rule that secures this is move-the-wrapper-then-access. +For example, use \tcode{*std::move(r)} or \tcode{std::move(r).error()}. +Never use access-then-move: \tcode{std::move(*r)} would create an rvalue reference to an object the wrapper does not own, making it possible to steal from the object the \tcode{expected} refers to. +Because it is a guarantee and not merely an implementation habit, it belongs in the design. + +The current wording spells the error of an rvalue \tcode{expected} as \tcode{std::move(error())}, which is access-then-move. +That was harmless while \tcode{E} could not be a reference. +The proposed wording replaces it with \tcode{std::move(unex).error()}, moving the exposition-only member, in \tcode{value}, \tcode{error_or}, and the monadic operations: the same type for an object \tcode{E}, and an lvalue for a reference \tcode{E}. -\section{Deleted functions carry messages (D10)} +\section{Deleted functions carry messages} Every deleted operation says what is wrong and what to do instead. For example, the deleted default constructor explains that \tcode{T\&} cannot be null. @@ -626,9 +645,9 @@ \section{Deleted functions carry messages (D10)} message text. \section{The held \tcode{unexpected} is exposition-only; the observable - behavior is keyed on \tcode{E} (D11)} + behavior is keyed on \tcode{E}} -Storing the error as \tcode{unexpected} (D4) is a specification device, not a +Storing the error as \tcode{unexpected} is a specification device, not a representation mandate. It supplies the reference case --- \tcode{unexpected} holds a pointer --- without a new specialization, and that is its only purpose. In particular it must not reach an existing implementation and tell it to change @@ -660,7 +679,7 @@ \section{The held \tcode{unexpected} is exposition-only; the observable described, never what the program is allowed to observe. \section{\tcode{expected} is trivially copyable when \tcode{T} and - \tcode{E} are (D12)} + \tcode{E} are} A drive-by. \tcode{expected} is not trivially copyable today, because \tcode{expected}'s copy and move assignment are specified as non-trivial @@ -738,7 +757,7 @@ \chapter{Wording} shallow-\tcode{const} observers. Marked edits in place widen the error type of the primary \tcode{expected} and of \tcode{expected} to admit an lvalue reference, and add \tcode{unexpected} with the reference-only -construction rule of D5. The Mandates of the primary template's +construction rule above. The Mandates of the primary template's \tcode{or_else} is restated so that it names \tcode{expected} rather than comparing \tcode{value_type}, which no longer separates \tcode{expected} from \tcode{expected}. The marked edits diff --git a/papers/expected-new.tex b/papers/expected-new.tex index 4838e3c7..4463c7ef 100644 --- a/papers/expected-new.tex +++ b/papers/expected-new.tex @@ -21,6 +21,9 @@ // \ref{expected.unexpected}, class template \tcode{unexpected} template class unexpected; + @\added{// \ref{expected.un.ref}, partial specialization of \tcode{unexpected} for lvalue reference types}@ + @\added{template class unexpected;}@ + // \ref{expected.bad}, class template \tcode{bad_expected_access} template class bad_expected_access; @@ -38,6 +41,9 @@ // \ref{expected.void}, partial specialization of \tcode{expected} for \tcode{void} types template requires is_void_v class expected; // partially freestanding + + @\added{// \ref{expected.ref}, partial specialization of \tcode{expected} for lvalue reference types}@ + @\added{template class expected; // partially freestanding}@ } \end{codeblock} @@ -284,7 +290,7 @@ \pnum A program that instantiates the definition of \tcode{unexpected} for -an array type or a specialization of \tcode{unexpected} is ill-formed. +a type that is not an object type, an array type, or a specialization of \tcode{unexpected} is ill-formed. Unlike the primary template, \tcode{E} may be a cv-qualified type. \indexlibraryctor{unexpected}% @@ -298,9 +304,8 @@ \constraints \begin{itemize} \item \tcode{is_same_v, unexpected>} is \tcode{false}, -\item \tcode{is_same_v, in_place_t>} is \tcode{false}, -\item \tcode{is_constructible_v} is \tcode{true}, and -\item \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\item \tcode{is_same_v, in_place_t>} is \tcode{false}, and +\item \tcode{is_constructible_v} is \tcode{true}. \end{itemize} \pnum @@ -310,9 +315,8 @@ \pnum \remarks -A constructor for which -\tcode{reference_constructs_from_temporary_v} is \tcode{true} --- -one that would bind \tcode{E\&} to a temporary --- is defined as deleted. +This constructor is defined as deleted if +\tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{unexpected}% @@ -324,13 +328,17 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_constructible_v} is \tcode{true} and -\tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \effects Initializes \exposid{unex} with \tcode{addressof(static_cast(std::forward(e)))}. + +\pnum +\remarks +This constructor is defined as deleted if +\tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{error}{unexpected}% @@ -643,7 +651,8 @@ if \tcode{remove_cv_t} is \tcode{void} or a complete non-array object type that is not \tcode{in_place_t}, \tcode{unexpect_t}, -or a specialization of \tcode{unexpected}. +or a specialization of \tcode{unexpected}\added{, +or if \tcode{T} is an lvalue reference to an object type that is not an array type\iref{expected.ref.general}}. A program which instantiates class template \tcode{expected} with an argument \tcode{T} that is not a valid value type for \tcode{expected} is ill-formed. @@ -748,7 +757,7 @@ If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{std::move(*rhs)}. Otherwise, -direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +direct-non-list-initializes \exposid{unex} with \tcode{\removed{std::move(rhs.error())}\added{std::move(rhs.\exposid{unex})}}. \pnum \ensures @@ -1158,7 +1167,7 @@ \item Otherwise, if \tcode{this->has_value()} is \tcode{true}, equivalent to: \begin{codeblock} -@\exposid{reinit-expected}@(@\exposid{unex}@, @\exposid{val}@, std::move(rhs.error())) +@\exposid{reinit-expected}@(@\exposid{unex}@, @\exposid{val}@, @\removed{std::move(rhs.error())}\added{std::move(rhs.\exposid{unex})}@) \end{codeblock} \item Otherwise, if \tcode{rhs.has_value()} is \tcode{true}, equivalent to: @@ -1355,7 +1364,7 @@ \item \tcode{is_swappable_v} is \tcode{true} and \item -\tcode{is_swappable_v} is \tcode{true} and +\tcode{is_swappable_v} is \tcode{true}\added{ or \tcode{is_reference_v} is \tcode{true}} and \item \tcode{is_move_constructible_v \&\& is_move_constructible_v} is \tcode{true}, and @@ -1506,7 +1515,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\removed{E}\added{remove_cv_t>}>} is \tcode{true}. \pnum \returns @@ -1526,8 +1535,8 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\removed{E}\added{remove_cv_t>}>} is \tcode{true} and +\tcode{is_constructible_v<\removed{E}\added{remove_cv_t>}, decltype(\removed{std::move(error())}\added{std::move(\exposid{unex}).error()})>} is \tcode{true}. \pnum \returns @@ -1535,7 +1544,7 @@ \pnum \throws -\tcode{bad_expected_access(std::move(error()))} +\tcode{bad_expected_access(\removed{std::move(error())}\added{std::move(\exposid{unex}).error()})} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} @@ -1640,7 +1649,7 @@ \pnum \returns \tcode{std::forward(e)} if \tcode{has_value()} is \tcode{true}, -\tcode{std::move(error())} otherwise. +\tcode{\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}} otherwise. \end{itemdescr} \rSec3[expected.object.monadic]{Monadic operations} @@ -1688,7 +1697,7 @@ \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates @@ -1702,7 +1711,7 @@ if (has_value()) return invoke(std::forward(f), std::move(@\exposid{val}@)); else - return U(unexpect, std::move(error())); + return U(unexpect, @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@); \end{codeblock} \end{itemdescr} @@ -1746,7 +1755,7 @@ \begin{itemdescr} \pnum Let \tcode{G} be -\tcode{remove_cvref_t>}. +\tcode{remove_cvref_t>}. \pnum \constraints @@ -1765,7 +1774,7 @@ if (has_value()) return G(in_place, std::move(@\exposid{val}@)); else - return invoke(std::forward(f), std::move(error())); + return invoke(std::forward(f), @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@); \end{codeblock} \end{itemdescr} @@ -1804,7 +1813,8 @@ Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with -\tcode{invoke(std::forward(f), \exposid{val})}. +\tcode{invoke(std::forward(f), \exposid{val})}\added{; +if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers}. \item Otherwise, evaluates \tcode{invoke(std::forward(f), \exposid{val})} and then returns \tcode{expected()}. @@ -1824,7 +1834,7 @@ \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates @@ -1840,12 +1850,13 @@ \begin{itemize} \item If \tcode{has_value()} is \tcode{false}, returns -\tcode{expected(unexpect, std::move(error()))}. +\tcode{expected(unexpect, \removed{std::move(error())}\added{std::move(\exposid{unex}).error()})}. \item Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with -\tcode{invoke(std::forward(f), std::move(\exposid{val}))}. +\tcode{invoke(std::forward(f), std::move(\exposid{val}))}\added{; +if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers}. \item Otherwise, evaluates \tcode{invoke(std::forward(f), std::move(\exposid{val}))} and then returns \tcode{ex\-pected()}. @@ -1892,7 +1903,7 @@ \begin{itemdescr} \pnum Let \tcode{G} be -\tcode{remove_cv_t>}. +\tcode{remove_cv_t>}. \pnum \constraints @@ -1903,7 +1914,7 @@ \tcode{G} is a valid template argument for \tcode{unexpected}\iref{expected.un.general} and the declaration \begin{codeblock} -G g(invoke(std::forward(f), std::move(error()))); +G g(invoke(std::forward(f), @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@)); \end{codeblock} is well-formed. @@ -1913,7 +1924,7 @@ \tcode{expected(in_place, std::move(\exposid{val}))}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with -\tcode{invoke(std::forward(f), std::move(error()))}. +\tcode{invoke(std::forward(f), \removed{std::move(error())}\added{std::move(\exposid{unex}).error()})}. \end{itemdescr} \rSec3[expected.object.eq]{Equality operators} @@ -2157,7 +2168,7 @@ \pnum \effects If \tcode{rhs.has_value()} is \tcode{false}, -direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +direct-non-list-initializes \exposid{unex} with \tcode{\removed{std::move(rhs.error())}\added{std::move(rhs.\exposid{unex})}}. \pnum \ensures @@ -2511,7 +2522,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_swappable_v} is \tcode{true} and +\tcode{is_swappable_v} is \tcode{true}\added{ or \tcode{is_reference_v} is \tcode{true}} and \tcode{is_move_constructible_v} is \tcode{true}. \pnum @@ -2594,7 +2605,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\removed{E}\added{remove_cv_t>}>} is \tcode{true}. \pnum \throws @@ -2609,12 +2620,12 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and -\tcode{is_move_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\removed{E}\added{remove_cv_t>}>} is \tcode{true} and +\tcode{is_move_constructible_v<\removed{E}\added{remove_cv_t>}>} is \tcode{true}. \pnum \throws -\tcode{bad_expected_access(std::move(error()))} +\tcode{bad_expected_access(\removed{std::move(error())}\added{std::move(\exposid{unex}).error()})} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} @@ -2687,7 +2698,7 @@ \pnum \returns \tcode{std::forward(e)} if \tcode{has_value()} is \tcode{true}, -\tcode{std::move(error())} otherwise. +\tcode{\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}} otherwise. \end{itemdescr} \rSec3[expected.void.monadic]{Monadic operations} @@ -2734,7 +2745,7 @@ \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates @@ -2748,7 +2759,7 @@ if (has_value()) return invoke(std::forward(f)); else - return U(unexpect, std::move(error())); + return U(unexpect, @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@); \end{codeblock} \end{itemdescr} @@ -2787,7 +2798,7 @@ \begin{itemdescr} \pnum Let \tcode{G} be -\tcode{remove_cvref_t>}. +\tcode{remove_cvref_t>}. \pnum \mandates @@ -2801,7 +2812,7 @@ if (has_value()) return G(); else - return invoke(std::forward(f), std::move(error())); + return invoke(std::forward(f), @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@); \end{codeblock} \end{itemdescr} @@ -2838,7 +2849,8 @@ Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with -\tcode{invoke(std::forward(f))}. +\tcode{invoke(std::forward(f))}\added{; +if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers}. \item Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. @@ -2857,7 +2869,7 @@ \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates @@ -2873,12 +2885,13 @@ \begin{itemize} \item If \tcode{has_value()} is \tcode{false}, returns -\tcode{expected(unexpect, std::move(error()))}. +\tcode{expected(unexpect, \removed{std::move(error())}\added{std::move(\exposid{unex}).error()})}. \item Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with -\tcode{invoke(std::forward(f))}. +\tcode{invoke(std::forward(f))}\added{; +if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers}. \item Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. @@ -2921,14 +2934,14 @@ \begin{itemdescr} \pnum Let \tcode{G} be -\tcode{remove_cv_t>}. +\tcode{remove_cv_t>}. \pnum \mandates \tcode{G} is a valid template argument for \tcode{unexpected}\iref{expected.un.general} and the declaration \begin{codeblock} -G g(invoke(std::forward(f), std::move(error()))); +G g(invoke(std::forward(f), @\removed{std::move(error())}\added{std::move(\exposid{unex}).error()}@)); \end{codeblock} is well-formed. @@ -2937,7 +2950,7 @@ If \tcode{has_value()} is \tcode{true}, \tcode{expected()}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with -\tcode{invoke(std::for\-ward(f), std::move(error()))}. +\tcode{invoke(std::for\-ward(f), \removed{std::move(error())}\added{std::move(\exposid{unex}).error()})}. \end{itemdescr} \rSec3[expected.void.eq]{Equality operators} @@ -3043,8 +3056,8 @@ constexpr T& operator*() const noexcept; constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; - constexpr T& value() const &; - constexpr T& value() &&; + constexpr T& value() const &; // freestanding-deleted + constexpr T& value() &&; // freestanding-deleted constexpr const E& error() const & noexcept; constexpr E& error() & noexcept; constexpr const E&& error() const && noexcept; @@ -3155,9 +3168,8 @@ \constraints \begin{itemize} \item \tcode{remove_cvref_t} is not \tcode{in_place_t}, -\tcode{expected}, or a specialization of \tcode{unexpected}; -\item \tcode{is_constructible_v} is \tcode{true}; and -\item \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{expected}, or a specialization of \tcode{unexpected}; and +\item \tcode{is_constructible_v} is \tcode{true}. \end{itemize} \pnum @@ -3173,9 +3185,8 @@ \remarks The expression inside \keyword{explicit} is equivalent to \tcode{!is_convertible_v}. -A constructor for which -\tcode{reference_constructs_from_temporary_v} is \tcode{true} --- -one that would bind \tcode{T\&} to a temporary --- is defined as deleted. +This constructor is defined as deleted if +\tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{expected}% @@ -3492,6 +3503,13 @@ \end{itemdecl} \begin{itemdescr} +\pnum +\mandates +\tcode{is_copy_constructible_v>>} is \tcode{true}. +For the second overload, +\tcode{is_constructible_v>, decltype(std::move(\exposid{unex}).error())>} +is also \tcode{true}. + \pnum \returns \tcode{*\exposid{val}} if \tcode{has_value()} is \tcode{true}. @@ -3501,7 +3519,7 @@ For the first overload, \tcode{bad_expected_access(as_const(error()))} if \tcode{has_value()} is \tcode{false}. For the second overload, -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. Both overloads yield \tcode{T\&}; the value category of \tcode{*this} does not affect the returned reference. \end{itemdescr} diff --git a/papers/wording/expected.tex b/papers/wording/expected.tex index b245cec5..457b8793 100644 --- a/papers/wording/expected.tex +++ b/papers/wording/expected.tex @@ -29,6 +29,9 @@ // \ref{expected.unexpected}, class template \tcode{unexpected} template class unexpected; + // \ref{expected.un.ref}, partial specialization of \tcode{unexpected} for lvalue reference types + template class unexpected; + // \ref{expected.bad}, class template \tcode{bad_expected_access} template class bad_expected_access; @@ -46,6 +49,9 @@ // \ref{expected.void}, partial specialization of \tcode{expected} for \tcode{void} types template requires is_void_v class expected; // partially freestanding + + // \ref{expected.ref}, partial specialization of \tcode{expected} for lvalue reference types + template class expected; // partially freestanding } \end{codeblock} @@ -65,18 +71,12 @@ constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; - template - constexpr explicit unexpected(Err&& e) noexcept(is_nothrow_constructible_v); + template constexpr explicit unexpected(Err&& e); - template - constexpr explicit unexpected(in_place_t, Args&&... args) noexcept( - is_nothrow_constructible_v); + template constexpr explicit unexpected(in_place_t, Args&&... args); template - constexpr explicit unexpected( - in_place_t, initializer_list il, - Args&&... args) noexcept(is_nothrow_constructible_v&, - Args...>); + constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args); constexpr unexpected& operator=(const unexpected&) = default; constexpr unexpected& operator=(unexpected&&) = default; @@ -91,25 +91,25 @@ template friend constexpr bool operator==(const unexpected& x, const unexpected& y); - friend constexpr void swap(unexpected& x, - unexpected& y) noexcept(noexcept(x.swap(y))); + friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))); private: E @\exposid{unex}@; // \expos }; + + template unexpected(E) -> unexpected; } \end{codeblock} \pnum A program that instantiates the definition of \tcode{unexpected} for a non-object type other than an lvalue reference type, an array type, a specialization of \tcode{unexpected}, or a cv-qualified type is ill-formed. - \rSec3[expected.un.cons]{Constructors} \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(Err&& e) noexcept(is_nothrow_constructible_v); + constexpr explicit unexpected(Err&& e); \end{itemdecl} \begin{itemdescr} @@ -133,7 +133,7 @@ \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(in_place_t, Args&&... args) noexcept(is_nothrow_constructible_v); + constexpr explicit unexpected(in_place_t, Args&&... args); \end{itemdecl} \begin{itemdescr} @@ -153,8 +153,7 @@ \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(in_place_t, initializer_list il, - Args&&... args) noexcept(is_nothrow_constructible_v&, Args...>); + constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} @@ -285,7 +284,6 @@ \pnum A program that instantiates the definition of \tcode{unexpected} for a referenced type that is not an object type, an array type, or a specialization of \tcode{unexpected} is ill-formed. Unlike the primary template, \tcode{E} may be a cv-qualified type. - \indexlibraryctor{unexpected}% \begin{itemdecl} template @@ -297,9 +295,8 @@ \constraints \begin{itemize} \item \tcode{is_same_v, unexpected>} is \tcode{false}, -\item \tcode{is_same_v, in_place_t>} is \tcode{false}, -\item \tcode{is_constructible_v} is \tcode{true}, and -\item \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\item \tcode{is_same_v, in_place_t>} is \tcode{false}, and +\item \tcode{is_constructible_v} is \tcode{true}. \end{itemize} \pnum @@ -308,7 +305,7 @@ \pnum \remarks -A constructor for which \tcode{reference_constructs_from_temporary_v} is \tcode{true} — one that would bind \tcode{E\&} to a temporary — is defined as deleted. +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{unexpected}% @@ -320,11 +317,15 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_constructible_v} is \tcode{true} and \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \effects Initializes \exposid{unex} with \tcode{addressof(static_cast(std::forward(e)))}. + +\pnum +\remarks +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{error}{unexpected}% @@ -387,8 +388,8 @@ template class @\libglobal{bad_expected_access}@ : public bad_expected_access { public: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION explicit bad_expected_access(E e); - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION const char* what() const noexcept override; + constexpr explicit bad_expected_access(E e); + constexpr const char* what() const noexcept override; constexpr E& error() & noexcept; constexpr const E& error() const& noexcept; constexpr E&& error() && noexcept; @@ -405,7 +406,7 @@ \indexlibraryctor{bad_expected_access}% \begin{itemdecl} -explicit bad_expected_access(E e); +constexpr explicit bad_expected_access(E e); \end{itemdecl} \begin{itemdescr} @@ -416,7 +417,7 @@ \indexlibrarymember{what}{bad_expected_access}% \begin{itemdecl} -const char* what() const noexcept override; +constexpr const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} @@ -456,26 +457,24 @@ template<> class @\libglobal{bad_expected_access}@ : public exception { protected: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access() noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access( - const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access( - bad_expected_access&&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=( - const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=( - bad_expected_access&&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION ~bad_expected_access() = default; + constexpr bad_expected_access() noexcept; + constexpr bad_expected_access(const bad_expected_access&) noexcept; + constexpr bad_expected_access(bad_expected_access&&) noexcept; + constexpr bad_expected_access& operator=( + const bad_expected_access&) noexcept; + constexpr bad_expected_access& operator=( + bad_expected_access&&) noexcept; + constexpr ~bad_expected_access(); public: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION const char* what() const noexcept override; + constexpr const char* what() const noexcept override; }; } \end{codeblock} \indexlibrarymember{what}{bad_expected_access}% \begin{itemdecl} -const char* what() const noexcept override; +constexpr const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} @@ -504,24 +503,19 @@ // \ref{expected.object.cons}, constructors - constexpr expected() noexcept(is_nothrow_default_constructible_v); + constexpr expected(); - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs); constexpr expected(expected&& rhs) noexcept(@\seebelow@); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); template> constexpr explicit(!is_convertible_v) expected(U&& v); @@ -529,12 +523,10 @@ template constexpr explicit(!is_convertible_v) expected(const unexpected& e); - template - constexpr explicit(!is_convertible_v) expected(unexpected&& e); + template constexpr explicit(!is_convertible_v) expected(unexpected&& e); template - constexpr explicit(!is_convertible_v) - expected(const unexpected& e) noexcept; + constexpr explicit(!is_convertible_v) expected(const unexpected& e) noexcept; template constexpr explicit(!is_convertible_v) expected(unexpected&& e) noexcept; @@ -571,9 +563,7 @@ // \ref{expected.object.assign}, assignment - constexpr expected& operator=(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v && - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v); + constexpr expected& operator=(const expected& rhs); constexpr expected& operator=(expected&& rhs) noexcept(@\seebelow@); @@ -631,11 +621,9 @@ template> constexpr T value_or(U&& def) &&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; // \ref{expected.object.monadic}, monadic operations @@ -665,8 +653,7 @@ requires(!is_void_v) friend constexpr bool operator==(const expected& x, const expected& y); - template - friend constexpr bool operator==(const expected& x, const T2& val); + template friend constexpr bool operator==(const expected& x, const T2& val); template friend constexpr bool operator==(const expected& x, const unexpected& e); @@ -688,17 +675,16 @@ When \tcode{has_value()} is \tcode{false}, the error is \tcode{\exposid{unex}.error()}. The error is held as an \tcode{unexpected}, and not as an \tcode{E}, so that \tcode{E} may be an lvalue reference type: an \tcode{E\&} cannot be a union member, whereas \tcode{unexpected} holds a pointer to an external object\iref{expected.un.ref}. \pnum -A type \tcode{T} is a \term{valid value type for \tcode{expected}}, if \tcode{remove_cv_t} is \tcode{void} or a complete non-array object type that is not \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}. A program which instantiates class template \tcode{expected} with an argument \tcode{T} that is not a valid value type for \tcode{expected} is ill-formed. A program that instantiates the definition of the template \tcode{expected} with a type for the \tcode{E} parameter that is not a valid template argument for \tcode{unexpected} is ill-formed. +A type \tcode{T} is a \term{valid value type for \tcode{expected}}, if \tcode{remove_cv_t} is \tcode{void} or a complete non-array object type that is not \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}, or if \tcode{T} is an lvalue reference to an object type that is not an array type\iref{expected.ref.general}. A program which instantiates class template \tcode{expected} with an argument \tcode{T} that is not a valid value type for \tcode{expected} is ill-formed. A program that instantiates the definition of the template \tcode{expected} with a type for the \tcode{E} parameter that is not a valid template argument for \tcode{unexpected} is ill-formed. \pnum When \tcode{T} is not \cv{} \tcode{void}, it shall meet the \oldconcept{Destructible} requirements (\tref{cpp17.destructible}). If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the \oldconcept{Destructible} requirements. - \rSec3[expected.object.cons]{Constructors} \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected() noexcept(is_nothrow_default_constructible_v); +constexpr expected(); \end{itemdecl} \begin{itemdescr} @@ -721,12 +707,10 @@ \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_constructible_v); +constexpr expected(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{*rhs}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{rhs.error()}. @@ -759,7 +743,7 @@ \pnum \effects -If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{std::move(*rhs)}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{std::move(*rhs)}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.\exposid{unex})}. \pnum \ensures @@ -1062,7 +1046,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{has_value()} is \tcode{true}, destroys \exposid{val}, otherwise destroys \exposid{unex}. @@ -1076,14 +1059,10 @@ \indexlibrarymember{operator=}{expected}% \begin{itemdecl} -constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v && - is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v); +constexpr expected& operator=(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = *rhs}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, rhs.error())}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, *rhs)}. Otherwise, equivalent to \tcode{\exposid{unex} = rhs.\exposid{unex}}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. @@ -1115,7 +1094,7 @@ \pnum \effects -If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = std::move(*rhs)}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, std::move(rhs.error()))}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, std::move(*rhs))}. Otherwise, equivalent to \tcode{\exposid{unex} = std::move(rhs.\exposid{unex})}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. +If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = std::move(*rhs)}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, std::move(rhs.\exposid{unex}))}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, std::move(*rhs))}. Otherwise, equivalent to \tcode{\exposid{unex} = std::move(rhs.\exposid{unex})}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. \pnum \returns @@ -1370,7 +1349,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \returns @@ -1390,7 +1369,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_constructible_v<\exposid{error-value-type}, decltype(std::move(\exposid{unex}).error())>} is \tcode{true}. \pnum \returns @@ -1398,7 +1377,7 @@ \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -1474,7 +1453,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_copy_constructible_v::\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v::\exposid{error-value-type}>} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v} is \tcode{true}. \pnum \mandates @@ -1494,7 +1473,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_move_constructible_v::\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v::\exposid{error-value-type}>} is \tcode{true}. +\tcode{is_move_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v} is \tcode{true}. \pnum \mandates @@ -1502,7 +1481,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. \end{itemdescr} \rSec3[expected.object.monadic]{Monadic operations} @@ -1516,17 +1495,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), \exposid{val}); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), \exposid{val}); else return U(unexpect, error());} \end{itemdescr} \indexlibrarymember{and_then}{expected}% @@ -1538,17 +1520,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), std::move(\exposid{val})); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), std::move(\exposid{val})); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -1560,17 +1545,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -1582,17 +1570,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -1604,13 +1595,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), \exposid{val}));} is well-formed. + \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, error()); else return expected(in_place, invoke(std::forward(f), \exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), \exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), \exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -1622,13 +1620,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. + +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), std::move(\exposid{val})));} is well-formed. \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, std::move(error())); else return expected(in_place, invoke(std::forward(f), std::move(\exposid{val})));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(\exposid{val}))}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), std::move(\exposid{val}))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -1640,6 +1645,9 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. @@ -1647,15 +1655,16 @@ \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(in_place, \exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -1667,6 +1676,9 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. @@ -1674,15 +1686,16 @@ \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return expected(unexpect, invoke(std::forward(f), std::move(error())));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(in_place, std::move(\exposid{val})); else return expected(unexpect, invoke(std::forward(f), std::move(\exposid{unex}).error()));} \end{itemdescr} \rSec3[expected.object.eq]{Equality operators} @@ -1762,14 +1775,12 @@ constexpr expected() noexcept; - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs); constexpr expected(expected&& rhs) noexcept(is_nothrow_move_constructible_v); template - constexpr explicit(!is_convertible_v) - expected(const expected& rhs); + constexpr explicit(!is_convertible_v) expected(const expected& rhs); template constexpr explicit(!is_convertible_v) expected(expected&& rhs); @@ -1777,12 +1788,10 @@ template constexpr explicit(!is_convertible_v) expected(const unexpected& e); - template - constexpr explicit(!is_convertible_v) expected(unexpected&& e); + template constexpr explicit(!is_convertible_v) expected(unexpected&& e); template - constexpr explicit(!is_convertible_v) - expected(const unexpected& e) noexcept; + constexpr explicit(!is_convertible_v) expected(const unexpected& e) noexcept; template constexpr explicit(!is_convertible_v) expected(unexpected&& e) noexcept; @@ -1811,8 +1820,7 @@ constexpr expected(unexpect_t, initializer_list, Args&&...) = delete; template - constexpr explicit(!is_convertible_v) - expected(const expected& rhs); + constexpr explicit(!is_convertible_v) expected(const expected& rhs); template constexpr explicit(!is_convertible_v) expected(expected&& rhs); @@ -1823,8 +1831,7 @@ // \ref{expected.void.assign}, assignment - constexpr expected& operator=(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v); + constexpr expected& operator=(const expected& rhs); constexpr expected& operator=(expected&& rhs) noexcept(@\seebelow@); @@ -1865,11 +1872,9 @@ constexpr const E&& error() const&& noexcept; constexpr E&& error() && noexcept; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; template constexpr void value_or(U&&) const = delete; @@ -1925,7 +1930,6 @@ \pnum If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the requirements of \oldconcept{Destructible} (\tref{cpp17.destructible}). - \rSec3[expected.void.cons]{Constructors} \indexlibraryctor{expected}% @@ -1941,11 +1945,10 @@ \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v); +constexpr expected(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{rhs.error()}. @@ -1975,7 +1978,7 @@ \pnum \effects -If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.\exposid{unex})}. \pnum \ensures @@ -2226,12 +2229,10 @@ \indexlibrarymember{operator=}{expected}% \begin{itemdecl} -constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v); +constexpr expected& operator=(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{this->has_value() \&\& rhs.has_value()}, no effects. Otherwise, if \tcode{this->has_value()}, equivalent to: \tcode{construct_at(addressof(\exposid{unex}), rhs.\exposid{unex}); \exposid{has_val} = false;} Otherwise, if \tcode{rhs.has_value()}, destroys \exposid{unex} and sets \exposid{has_val} to \tcode{true}. Otherwise, equivalent to \tcode{\exposid{unex} = rhs.\exposid{unex}}. @@ -2424,7 +2425,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \throws @@ -2439,11 +2440,11 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_move_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_move_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -2515,7 +2516,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. \end{itemdescr} \indexlibrarymember{value_or}{expected}% @@ -2545,17 +2546,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, error());} \end{itemdescr} \indexlibrarymember{and_then}{expected}% @@ -2567,17 +2571,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -2589,13 +2596,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. +\tcode{G} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -2607,13 +2617,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. +\tcode{G} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -2625,17 +2638,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{U} is a valid value type for \tcode{expected}, where \tcode{U} is \tcode{remove_cv_t>}. +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f)));} is well-formed. \pnum \effects -If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}; if \tcode{U} is an lvalue reference type, \tcode{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -2647,17 +2663,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{U} is a valid value type for \tcode{expected}, where \tcode{U} is \tcode{remove_cv_t>}. +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f)));} is well-formed. \pnum \effects -If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(error()))}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}; if \tcode{U} is an lvalue reference type, \tcode{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -2669,9 +2688,12 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates -\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), error()));} is well-formed, where \tcode{G} is \tcode{remove_cv_t>}. +\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), error()));} is well-formed. \pnum \returns @@ -2687,13 +2709,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates -\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), std::move(error())));} is well-formed, where \tcode{G} is \tcode{remove_cv_t>}. +\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), std::move(\exposid{unex}).error()));} is well-formed. \pnum \returns -If \tcode{has_value()} is \tcode{true}, \tcode{expected()}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(error()))}. +If \tcode{has_value()} is \tcode{true}, \tcode{expected()}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(\exposid{unex}).error())}. \end{itemdescr} \rSec3[expected.void.eq]{Equality operators} @@ -2753,37 +2778,28 @@ expected() = delete; - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v); constexpr expected(expected&& rhs) noexcept(@\seebelow@); template constexpr expected(in_place_t, Args&&...) = delete; template - constexpr explicit(@\seebelow@) - expected(U&& u) noexcept(is_nothrow_constructible_v); + constexpr explicit(@\seebelow@) expected(U&& u) noexcept(is_nothrow_constructible_v); - template constexpr expected(U&&) = delete; - - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); template constexpr explicit(@\seebelow@) expected(const unexpected& e); template constexpr explicit(@\seebelow@) expected(unexpected&& e); - template - constexpr explicit(@\seebelow@) expected(const unexpected& e) noexcept; + template constexpr explicit(@\seebelow@) expected(const unexpected& e) noexcept; template constexpr explicit(@\seebelow@) expected(unexpected&& e) noexcept; @@ -2851,8 +2867,8 @@ constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; - constexpr T& value() const&; - constexpr T& value() &&; + constexpr T& value() const&; // freestanding-deleted + constexpr T& value() &&; // freestanding-deleted constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; @@ -2861,11 +2877,9 @@ template> constexpr remove_cv_t value_or(U&& def) const; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; // \ref{expected.ref.monadic}, monadic operations @@ -2895,8 +2909,7 @@ requires(!is_void_v) friend constexpr bool operator==(const expected& x, const expected& y); - template - friend constexpr bool operator==(const expected& x, const T2& val); + template friend constexpr bool operator==(const expected& x, const T2& val); template friend constexpr bool operator==(const expected& x, const unexpected& e); @@ -2923,7 +2936,6 @@ \pnum \tcode{T} shall be an object type that is not an array type, \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}. If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the \oldconcept{Destructible} requirements (\tref{cpp17.destructible}). - \rSec3[expected.ref.cons]{Constructors} \indexlibraryctor{expected}% @@ -2944,7 +2956,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}, initializes \exposid{val} with \tcode{rhs.\exposid{val}}, so that \tcode{*this} and \tcode{rhs} refer to the same object; otherwise, initializes \exposid{unex} with \tcode{rhs.\exposid{unex}}. @@ -2979,7 +2990,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{remove_cvref_t} is not \tcode{in_place_t}, \tcode{expected}, or a specialization of \tcode{unexpected}; \tcode{is_constructible_v} is \tcode{true}; and \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{remove_cvref_t} is not \tcode{in_place_t}, \tcode{expected}, or a specialization of \tcode{unexpected}; and \tcode{is_constructible_v} is \tcode{true}. \pnum \effects @@ -2988,22 +2999,10 @@ \pnum \ensures \tcode{has_value()} is \tcode{true}. -\end{itemdescr} - -\indexlibraryctor{expected}% -\begin{itemdecl} -template - constexpr expected(U&&) = delete; -\end{itemdecl} - -\begin{itemdescr} -\pnum -\constraints -\tcode{reference_constructs_from_temporary_v} is \tcode{true}. \pnum \remarks -A constructor for which \tcode{reference_constructs_from_temporary_v} is \tcode{true} — one that would bind \tcode{T\&} to a temporary — is defined as deleted. +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{expected}% @@ -3205,6 +3204,9 @@ \rSec3[expected.ref.assign]{Assignment} +\pnum +Assignment rebinds: assigning to an \tcode{expected} that holds a value changes which object it refers to. It never assigns through to the referent. + \indexlibrarymember{operator=}{expected}% \begin{itemdecl} constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && @@ -3213,7 +3215,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}: if \tcode{has_value()} is \tcode{true}, assigns \tcode{rhs.\exposid{val}} to \exposid{val}; otherwise destroys \exposid{unex} and initializes \exposid{val} with \tcode{rhs.\exposid{val}}. If \tcode{rhs.has_value()} is \tcode{false}, the error of \tcode{rhs} is assigned to or used to initialize \exposid{unex}, as for the primary template. In every case \tcode{*this} comes to refer to the object \tcode{rhs} refers to, or to hold the error of \tcode{rhs}. @@ -3331,9 +3332,6 @@ \tcode{*\exposid{val}}. \end{itemdescr} -\pnum -Assignment rebinds: assigning to an \tcode{expected} that holds a value changes which object it refers to. It never assigns through to the referent. - \rSec3[expected.ref.swap]{Swap} \indexlibrarymember{swap}{expected}% @@ -3448,13 +3446,17 @@ \end{itemdecl} \begin{itemdescr} +\pnum +\mandates +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_constructible_v<\exposid{error-value-type}, decltype(std::move(\exposid{unex}).error())>} is \tcode{true}. + \pnum \returns \tcode{*\exposid{val}} if \tcode{has_value()} is \tcode{true}. \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -3496,17 +3498,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{X} be \tcode{remove_cv_t}. + \pnum \constraints \tcode{is_object_v} is \tcode{true} and \tcode{is_array_v} is \tcode{false}. \pnum \mandates -\tcode{is_convertible_v>} and \tcode{is_convertible_v>} are \tcode{true}. +\tcode{is_convertible_v} and \tcode{is_convertible_v} are \tcode{true}. \pnum \returns -\tcode{has_value() ? static_cast>(*\exposid{val}) : static_cast>(std::forward(def))}. The result is an object, never a reference. +\tcode{has_value() ? static_cast(*\exposid{val}) : static_cast(std::forward(def))}. The result is an object, never a reference. \end{itemdescr} \indexlibrarymember{error_or}{expected}% @@ -3538,7 +3543,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. The result is an object, never a reference. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. The result is an object, never a reference. \end{itemdescr} \rSec3[expected.ref.monadic]{Monadic operations} @@ -3552,17 +3557,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, error());} \pnum \remarks @@ -3578,17 +3586,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -3600,13 +3611,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -3618,13 +3632,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -3636,13 +3653,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), *\exposid{val}));} is well-formed. + \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, error()); else return expected(in_place, invoke(std::forward(f), *\exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), *\exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), *\exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -3654,13 +3678,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. + +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), *\exposid{val}));} is well-formed. \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, std::move(error())); else return expected(in_place, invoke(std::forward(f), *\exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), *\exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), *\exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -3672,18 +3703,22 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -3695,18 +3730,22 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), std::move(error())));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), std::move(\exposid{unex}).error()));} \end{itemdescr} \rSec3[expected.ref.eq]{Equality operators} diff --git a/papers/wording/fragments/expected.bad.tex b/papers/wording/fragments/expected.bad.tex index 4c436d5e..c2a8ea7f 100644 --- a/papers/wording/fragments/expected.bad.tex +++ b/papers/wording/fragments/expected.bad.tex @@ -5,8 +5,8 @@ template class @\libglobal{bad_expected_access}@ : public bad_expected_access { public: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION explicit bad_expected_access(E e); - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION const char* what() const noexcept override; + constexpr explicit bad_expected_access(E e); + constexpr const char* what() const noexcept override; constexpr E& error() & noexcept; constexpr const E& error() const& noexcept; constexpr E&& error() && noexcept; @@ -23,7 +23,7 @@ \indexlibraryctor{bad_expected_access}% \begin{itemdecl} -explicit bad_expected_access(E e); +constexpr explicit bad_expected_access(E e); \end{itemdecl} \begin{itemdescr} @@ -34,7 +34,7 @@ \indexlibrarymember{what}{bad_expected_access}% \begin{itemdecl} -const char* what() const noexcept override; +constexpr const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} diff --git a/papers/wording/fragments/expected.bad.void.tex b/papers/wording/fragments/expected.bad.void.tex index bafa2d27..c554f118 100644 --- a/papers/wording/fragments/expected.bad.void.tex +++ b/papers/wording/fragments/expected.bad.void.tex @@ -5,26 +5,24 @@ template<> class @\libglobal{bad_expected_access}@ : public exception { protected: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access() noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access( - const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access( - bad_expected_access&&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=( - const bad_expected_access&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION bad_expected_access& operator=( - bad_expected_access&&) noexcept = default; - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION ~bad_expected_access() = default; + constexpr bad_expected_access() noexcept; + constexpr bad_expected_access(const bad_expected_access&) noexcept; + constexpr bad_expected_access(bad_expected_access&&) noexcept; + constexpr bad_expected_access& operator=( + const bad_expected_access&) noexcept; + constexpr bad_expected_access& operator=( + bad_expected_access&&) noexcept; + constexpr ~bad_expected_access(); public: - BEMAN_EXPECTED_CONSTEXPR_EXCEPTION const char* what() const noexcept override; + constexpr const char* what() const noexcept override; }; } \end{codeblock} \indexlibrarymember{what}{bad_expected_access}% \begin{itemdecl} -const char* what() const noexcept override; +constexpr const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} diff --git a/papers/wording/fragments/expected.expected.tex b/papers/wording/fragments/expected.expected.tex index 7413de25..824aff6b 100644 --- a/papers/wording/fragments/expected.expected.tex +++ b/papers/wording/fragments/expected.expected.tex @@ -18,24 +18,19 @@ // \ref{expected.object.cons}, constructors - constexpr expected() noexcept(is_nothrow_default_constructible_v); + constexpr expected(); - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs); constexpr expected(expected&& rhs) noexcept(@\seebelow@); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); template> constexpr explicit(!is_convertible_v) expected(U&& v); @@ -43,12 +38,10 @@ template constexpr explicit(!is_convertible_v) expected(const unexpected& e); - template - constexpr explicit(!is_convertible_v) expected(unexpected&& e); + template constexpr explicit(!is_convertible_v) expected(unexpected&& e); template - constexpr explicit(!is_convertible_v) - expected(const unexpected& e) noexcept; + constexpr explicit(!is_convertible_v) expected(const unexpected& e) noexcept; template constexpr explicit(!is_convertible_v) expected(unexpected&& e) noexcept; @@ -85,9 +78,7 @@ // \ref{expected.object.assign}, assignment - constexpr expected& operator=(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v && - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v); + constexpr expected& operator=(const expected& rhs); constexpr expected& operator=(expected&& rhs) noexcept(@\seebelow@); @@ -145,11 +136,9 @@ template> constexpr T value_or(U&& def) &&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; // \ref{expected.object.monadic}, monadic operations @@ -179,8 +168,7 @@ requires(!is_void_v) friend constexpr bool operator==(const expected& x, const expected& y); - template - friend constexpr bool operator==(const expected& x, const T2& val); + template friend constexpr bool operator==(const expected& x, const T2& val); template friend constexpr bool operator==(const expected& x, const unexpected& e); @@ -202,17 +190,16 @@ When \tcode{has_value()} is \tcode{false}, the error is \tcode{\exposid{unex}.error()}. The error is held as an \tcode{unexpected}, and not as an \tcode{E}, so that \tcode{E} may be an lvalue reference type: an \tcode{E\&} cannot be a union member, whereas \tcode{unexpected} holds a pointer to an external object\iref{expected.un.ref}. \pnum -A type \tcode{T} is a \term{valid value type for \tcode{expected}}, if \tcode{remove_cv_t} is \tcode{void} or a complete non-array object type that is not \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}. A program which instantiates class template \tcode{expected} with an argument \tcode{T} that is not a valid value type for \tcode{expected} is ill-formed. A program that instantiates the definition of the template \tcode{expected} with a type for the \tcode{E} parameter that is not a valid template argument for \tcode{unexpected} is ill-formed. +A type \tcode{T} is a \term{valid value type for \tcode{expected}}, if \tcode{remove_cv_t} is \tcode{void} or a complete non-array object type that is not \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}, or if \tcode{T} is an lvalue reference to an object type that is not an array type\iref{expected.ref.general}. A program which instantiates class template \tcode{expected} with an argument \tcode{T} that is not a valid value type for \tcode{expected} is ill-formed. A program that instantiates the definition of the template \tcode{expected} with a type for the \tcode{E} parameter that is not a valid template argument for \tcode{unexpected} is ill-formed. \pnum When \tcode{T} is not \cv{} \tcode{void}, it shall meet the \oldconcept{Destructible} requirements (\tref{cpp17.destructible}). If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the \oldconcept{Destructible} requirements. - \rSec3[expected.object.cons]{Constructors} \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected() noexcept(is_nothrow_default_constructible_v); +constexpr expected(); \end{itemdecl} \begin{itemdescr} @@ -235,12 +222,10 @@ \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_constructible_v); +constexpr expected(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{*rhs}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{rhs.error()}. @@ -273,7 +258,7 @@ \pnum \effects -If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{std::move(*rhs)}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +If \tcode{rhs.has_value()} is \tcode{true}, direct-non-list-initializes \exposid{val} with \tcode{std::move(*rhs)}. Otherwise, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.\exposid{unex})}. \pnum \ensures @@ -576,7 +561,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{has_value()} is \tcode{true}, destroys \exposid{val}, otherwise destroys \exposid{unex}. @@ -590,14 +574,10 @@ \indexlibrarymember{operator=}{expected}% \begin{itemdecl} -constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v && - is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v); +constexpr expected& operator=(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = *rhs}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, rhs.error())}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, *rhs)}. Otherwise, equivalent to \tcode{\exposid{unex} = rhs.\exposid{unex}}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. @@ -629,7 +609,7 @@ \pnum \effects -If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = std::move(*rhs)}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, std::move(rhs.error()))}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, std::move(*rhs))}. Otherwise, equivalent to \tcode{\exposid{unex} = std::move(rhs.\exposid{unex})}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. +If \tcode{this->has_value() \&\& rhs.has_value()}, equivalent to \tcode{\exposid{val} = std::move(*rhs)}. Otherwise, if \tcode{this->has_value()}, equivalent to \tcode{reinit-expected(\exposid{unex}, \exposid{val}, std::move(rhs.\exposid{unex}))}. Otherwise, if \tcode{rhs.has_value()}, equivalent to \tcode{reinit-expected(\exposid{val}, \exposid{unex}, std::move(*rhs))}. Otherwise, equivalent to \tcode{\exposid{unex} = std::move(rhs.\exposid{unex})}. Then, if no exception was thrown, equivalent to: \tcode{\exposid{has_val} = rhs.has_value(); return *this;} When \tcode{E} is an lvalue reference type, each of the cases above that initializes or assigns \exposid{unex} rebinds it: \exposid{unex} comes to refer to the same object as \tcode{rhs}'s error. No previously or subsequently referenced object is assigned through. \pnum \returns @@ -884,7 +864,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \returns @@ -904,7 +884,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_constructible_v<\exposid{error-value-type}, decltype(std::move(\exposid{unex}).error())>} is \tcode{true}. \pnum \returns @@ -912,7 +892,7 @@ \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -988,7 +968,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_copy_constructible_v::\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v::\exposid{error-value-type}>} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v} is \tcode{true}. \pnum \mandates @@ -1008,7 +988,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_move_constructible_v::\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v::\exposid{error-value-type}>} is \tcode{true}. +\tcode{is_move_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_convertible_v} is \tcode{true}. \pnum \mandates @@ -1016,7 +996,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. \end{itemdescr} \rSec3[expected.object.monadic]{Monadic operations} @@ -1030,17 +1010,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), \exposid{val}); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), \exposid{val}); else return U(unexpect, error());} \end{itemdescr} \indexlibrarymember{and_then}{expected}% @@ -1052,17 +1035,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), std::move(\exposid{val})); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), std::move(\exposid{val})); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -1074,17 +1060,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -1096,17 +1085,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -1118,13 +1110,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), \exposid{val}));} is well-formed. + \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, error()); else return expected(in_place, invoke(std::forward(f), \exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), \exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), \exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -1136,13 +1135,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. + +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), std::move(\exposid{val})));} is well-formed. \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, std::move(error())); else return expected(in_place, invoke(std::forward(f), std::move(\exposid{val})));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(\exposid{val}))}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), std::move(\exposid{val}))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -1154,6 +1160,9 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. @@ -1161,15 +1170,16 @@ \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, \exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(in_place, \exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -1181,6 +1191,9 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. @@ -1188,15 +1201,16 @@ \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(in_place, std::move(\exposid{val})); else return expected(unexpect, invoke(std::forward(f), std::move(error())));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(in_place, std::move(\exposid{val})); else return expected(unexpect, invoke(std::forward(f), std::move(\exposid{unex}).error()));} \end{itemdescr} \rSec3[expected.object.eq]{Equality operators} diff --git a/papers/wording/fragments/expected.ref.tex b/papers/wording/fragments/expected.ref.tex index 4aa0bff1..924a88a3 100644 --- a/papers/wording/fragments/expected.ref.tex +++ b/papers/wording/fragments/expected.ref.tex @@ -20,37 +20,28 @@ expected() = delete; - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v); constexpr expected(expected&& rhs) noexcept(@\seebelow@); template constexpr expected(in_place_t, Args&&...) = delete; template - constexpr explicit(@\seebelow@) - expected(U&& u) noexcept(is_nothrow_constructible_v); + constexpr explicit(@\seebelow@) expected(U&& u) noexcept(is_nothrow_constructible_v); - template constexpr expected(U&&) = delete; + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(const expected& rhs); - template - constexpr explicit(@\seebelow@) expected(const expected& rhs); - - template - constexpr explicit(@\seebelow@) expected(expected&& rhs); + template constexpr explicit(@\seebelow@) expected(expected&& rhs); template constexpr explicit(@\seebelow@) expected(const unexpected& e); template constexpr explicit(@\seebelow@) expected(unexpected&& e); - template - constexpr explicit(@\seebelow@) expected(const unexpected& e) noexcept; + template constexpr explicit(@\seebelow@) expected(const unexpected& e) noexcept; template constexpr explicit(@\seebelow@) expected(unexpected&& e) noexcept; @@ -118,8 +109,8 @@ constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; - constexpr T& value() const&; - constexpr T& value() &&; + constexpr T& value() const&; // freestanding-deleted + constexpr T& value() &&; // freestanding-deleted constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; @@ -128,11 +119,9 @@ template> constexpr remove_cv_t value_or(U&& def) const; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; // \ref{expected.ref.monadic}, monadic operations @@ -162,8 +151,7 @@ requires(!is_void_v) friend constexpr bool operator==(const expected& x, const expected& y); - template - friend constexpr bool operator==(const expected& x, const T2& val); + template friend constexpr bool operator==(const expected& x, const T2& val); template friend constexpr bool operator==(const expected& x, const unexpected& e); @@ -190,7 +178,6 @@ \pnum \tcode{T} shall be an object type that is not an array type, \tcode{in_place_t}, \tcode{unexpect_t}, or a specialization of \tcode{unexpected}. If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the \oldconcept{Destructible} requirements (\tref{cpp17.destructible}). - \rSec3[expected.ref.cons]{Constructors} \indexlibraryctor{expected}% @@ -211,7 +198,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}, initializes \exposid{val} with \tcode{rhs.\exposid{val}}, so that \tcode{*this} and \tcode{rhs} refer to the same object; otherwise, initializes \exposid{unex} with \tcode{rhs.\exposid{unex}}. @@ -246,7 +232,7 @@ \begin{itemdescr} \pnum \constraints -\tcode{remove_cvref_t} is not \tcode{in_place_t}, \tcode{expected}, or a specialization of \tcode{unexpected}; \tcode{is_constructible_v} is \tcode{true}; and \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{remove_cvref_t} is not \tcode{in_place_t}, \tcode{expected}, or a specialization of \tcode{unexpected}; and \tcode{is_constructible_v} is \tcode{true}. \pnum \effects @@ -255,22 +241,10 @@ \pnum \ensures \tcode{has_value()} is \tcode{true}. -\end{itemdescr} - -\indexlibraryctor{expected}% -\begin{itemdecl} -template - constexpr expected(U&&) = delete; -\end{itemdecl} - -\begin{itemdescr} -\pnum -\constraints -\tcode{reference_constructs_from_temporary_v} is \tcode{true}. \pnum \remarks -A constructor for which \tcode{reference_constructs_from_temporary_v} is \tcode{true} — one that would bind \tcode{T\&} to a temporary — is defined as deleted. +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{expected}% @@ -472,6 +446,9 @@ \rSec3[expected.ref.assign]{Assignment} +\pnum +Assignment rebinds: assigning to an \tcode{expected} that holds a value changes which object it refers to. It never assigns through to the referent. + \indexlibrarymember{operator=}{expected}% \begin{itemdecl} constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && @@ -480,7 +457,6 @@ \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{true}: if \tcode{has_value()} is \tcode{true}, assigns \tcode{rhs.\exposid{val}} to \exposid{val}; otherwise destroys \exposid{unex} and initializes \exposid{val} with \tcode{rhs.\exposid{val}}. If \tcode{rhs.has_value()} is \tcode{false}, the error of \tcode{rhs} is assigned to or used to initialize \exposid{unex}, as for the primary template. In every case \tcode{*this} comes to refer to the object \tcode{rhs} refers to, or to hold the error of \tcode{rhs}. @@ -598,9 +574,6 @@ \tcode{*\exposid{val}}. \end{itemdescr} -\pnum -Assignment rebinds: assigning to an \tcode{expected} that holds a value changes which object it refers to. It never assigns through to the referent. - \rSec3[expected.ref.swap]{Swap} \indexlibrarymember{swap}{expected}% @@ -715,13 +688,17 @@ \end{itemdecl} \begin{itemdescr} +\pnum +\mandates +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_constructible_v<\exposid{error-value-type}, decltype(std::move(\exposid{unex}).error())>} is \tcode{true}. + \pnum \returns \tcode{*\exposid{val}} if \tcode{has_value()} is \tcode{true}. \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -763,17 +740,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{X} be \tcode{remove_cv_t}. + \pnum \constraints \tcode{is_object_v} is \tcode{true} and \tcode{is_array_v} is \tcode{false}. \pnum \mandates -\tcode{is_convertible_v>} and \tcode{is_convertible_v>} are \tcode{true}. +\tcode{is_convertible_v} and \tcode{is_convertible_v} are \tcode{true}. \pnum \returns -\tcode{has_value() ? static_cast>(*\exposid{val}) : static_cast>(std::forward(def))}. The result is an object, never a reference. +\tcode{has_value() ? static_cast(*\exposid{val}) : static_cast(std::forward(def))}. The result is an object, never a reference. \end{itemdescr} \indexlibrarymember{error_or}{expected}% @@ -805,7 +785,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. The result is an object, never a reference. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. The result is an object, never a reference. \end{itemdescr} \rSec3[expected.ref.monadic]{Monadic operations} @@ -819,17 +799,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, error());} \pnum \remarks @@ -845,17 +828,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f), *\exposid{val}); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -867,13 +853,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -885,13 +874,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is \tcode{expected} for some type \tcode{X}. +\tcode{G} is \tcode{expected} for some type \tcode{X}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -903,13 +895,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), *\exposid{val}));} is well-formed. + \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, error()); else return expected(in_place, invoke(std::forward(f), *\exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), *\exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), *\exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -921,13 +920,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. + +\pnum +\mandates +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f), *\exposid{val}));} is well-formed. \pnum \effects -Equivalent to: \tcode{if (!has_value()) return U(unexpect, std::move(error())); else return expected(in_place, invoke(std::forward(f), *\exposid{val}));} where \tcode{U2} is \tcode{remove_cv_t>} and \tcode{U} is \tcode{expected}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \exposid{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), *\exposid{val})}; if \tcode{U} is an lvalue reference type, \exposid{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f), *\exposid{val})} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -939,18 +945,22 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), error()));} \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -962,18 +972,22 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates \begin{itemize} -\item \tcode{is_object_v} is \tcode{true}, -\item \tcode{is_array_v} is \tcode{false}, +\item \tcode{is_object_v>} is \tcode{true}, +\item \tcode{is_rvalue_reference_v} is \tcode{false}, +\item \tcode{is_array_v>} is \tcode{false}, \item \tcode{is_same_v>} is \tcode{true}, -\item \tcode{\exposid{is-unexpected-specialization}::value} is \tcode{false}. +\item \tcode{\exposid{is-unexpected-specialization}>::value} is \tcode{false}. \end{itemize} \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), std::move(error())));} where \tcode{G2} is \tcode{remove_cv_t>} and \tcode{G} is \tcode{expected}. +Equivalent to: \tcode{if (has_value()) return expected(*\exposid{val}); else return expected(unexpect, invoke(std::forward(f), std::move(\exposid{unex}).error()));} \end{itemdescr} \rSec3[expected.ref.eq]{Equality operators} diff --git a/papers/wording/fragments/expected.syn.tex b/papers/wording/fragments/expected.syn.tex index f392ffa6..83155113 100644 --- a/papers/wording/fragments/expected.syn.tex +++ b/papers/wording/fragments/expected.syn.tex @@ -6,6 +6,9 @@ // \ref{expected.unexpected}, class template \tcode{unexpected} template class unexpected; + // \ref{expected.un.ref}, partial specialization of \tcode{unexpected} for lvalue reference types + template class unexpected; + // \ref{expected.bad}, class template \tcode{bad_expected_access} template class bad_expected_access; @@ -23,5 +26,8 @@ // \ref{expected.void}, partial specialization of \tcode{expected} for \tcode{void} types template requires is_void_v class expected; // partially freestanding + + // \ref{expected.ref}, partial specialization of \tcode{expected} for lvalue reference types + template class expected; // partially freestanding } \end{codeblock} diff --git a/papers/wording/fragments/expected.unexpected.tex b/papers/wording/fragments/expected.unexpected.tex index d582d8d5..2f00d049 100644 --- a/papers/wording/fragments/expected.unexpected.tex +++ b/papers/wording/fragments/expected.unexpected.tex @@ -14,18 +14,12 @@ constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; - template - constexpr explicit unexpected(Err&& e) noexcept(is_nothrow_constructible_v); + template constexpr explicit unexpected(Err&& e); - template - constexpr explicit unexpected(in_place_t, Args&&... args) noexcept( - is_nothrow_constructible_v); + template constexpr explicit unexpected(in_place_t, Args&&... args); template - constexpr explicit unexpected( - in_place_t, initializer_list il, - Args&&... args) noexcept(is_nothrow_constructible_v&, - Args...>); + constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args); constexpr unexpected& operator=(const unexpected&) = default; constexpr unexpected& operator=(unexpected&&) = default; @@ -40,25 +34,25 @@ template friend constexpr bool operator==(const unexpected& x, const unexpected& y); - friend constexpr void swap(unexpected& x, - unexpected& y) noexcept(noexcept(x.swap(y))); + friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))); private: E @\exposid{unex}@; // \expos }; + + template unexpected(E) -> unexpected; } \end{codeblock} \pnum A program that instantiates the definition of \tcode{unexpected} for a non-object type other than an lvalue reference type, an array type, a specialization of \tcode{unexpected}, or a cv-qualified type is ill-formed. - \rSec3[expected.un.cons]{Constructors} \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(Err&& e) noexcept(is_nothrow_constructible_v); + constexpr explicit unexpected(Err&& e); \end{itemdecl} \begin{itemdescr} @@ -82,7 +76,7 @@ \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(in_place_t, Args&&... args) noexcept(is_nothrow_constructible_v); + constexpr explicit unexpected(in_place_t, Args&&... args); \end{itemdecl} \begin{itemdescr} @@ -102,8 +96,7 @@ \indexlibraryctor{unexpected}% \begin{itemdecl} template - constexpr explicit unexpected(in_place_t, initializer_list il, - Args&&... args) noexcept(is_nothrow_constructible_v&, Args...>); + constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} @@ -234,7 +227,6 @@ \pnum A program that instantiates the definition of \tcode{unexpected} for a referenced type that is not an object type, an array type, or a specialization of \tcode{unexpected} is ill-formed. Unlike the primary template, \tcode{E} may be a cv-qualified type. - \indexlibraryctor{unexpected}% \begin{itemdecl} template @@ -246,9 +238,8 @@ \constraints \begin{itemize} \item \tcode{is_same_v, unexpected>} is \tcode{false}, -\item \tcode{is_same_v, in_place_t>} is \tcode{false}, -\item \tcode{is_constructible_v} is \tcode{true}, and -\item \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\item \tcode{is_same_v, in_place_t>} is \tcode{false}, and +\item \tcode{is_constructible_v} is \tcode{true}. \end{itemize} \pnum @@ -257,7 +248,7 @@ \pnum \remarks -A constructor for which \tcode{reference_constructs_from_temporary_v} is \tcode{true} — one that would bind \tcode{E\&} to a temporary — is defined as deleted. +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibraryctor{unexpected}% @@ -269,11 +260,15 @@ \begin{itemdescr} \pnum \constraints -\tcode{is_constructible_v} is \tcode{true} and \tcode{reference_constructs_from_temporary_v} is \tcode{false}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \effects Initializes \exposid{unex} with \tcode{addressof(static_cast(std::forward(e)))}. + +\pnum +\remarks +This constructor is defined as deleted if \tcode{reference_constructs_from_temporary_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{error}{unexpected}% diff --git a/papers/wording/fragments/expected.void.tex b/papers/wording/fragments/expected.void.tex index 74687377..722f784f 100644 --- a/papers/wording/fragments/expected.void.tex +++ b/papers/wording/fragments/expected.void.tex @@ -20,14 +20,12 @@ constexpr expected() noexcept; - constexpr expected(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v); + constexpr expected(const expected& rhs); constexpr expected(expected&& rhs) noexcept(is_nothrow_move_constructible_v); template - constexpr explicit(!is_convertible_v) - expected(const expected& rhs); + constexpr explicit(!is_convertible_v) expected(const expected& rhs); template constexpr explicit(!is_convertible_v) expected(expected&& rhs); @@ -35,12 +33,10 @@ template constexpr explicit(!is_convertible_v) expected(const unexpected& e); - template - constexpr explicit(!is_convertible_v) expected(unexpected&& e); + template constexpr explicit(!is_convertible_v) expected(unexpected&& e); template - constexpr explicit(!is_convertible_v) - expected(const unexpected& e) noexcept; + constexpr explicit(!is_convertible_v) expected(const unexpected& e) noexcept; template constexpr explicit(!is_convertible_v) expected(unexpected&& e) noexcept; @@ -69,8 +65,7 @@ constexpr expected(unexpect_t, initializer_list, Args&&...) = delete; template - constexpr explicit(!is_convertible_v) - expected(const expected& rhs); + constexpr explicit(!is_convertible_v) expected(const expected& rhs); template constexpr explicit(!is_convertible_v) expected(expected&& rhs); @@ -81,8 +76,7 @@ // \ref{expected.void.assign}, assignment - constexpr expected& operator=(const expected& rhs) noexcept( - is_nothrow_copy_constructible_v && is_nothrow_copy_assignable_v); + constexpr expected& operator=(const expected& rhs); constexpr expected& operator=(expected&& rhs) noexcept(@\seebelow@); @@ -123,11 +117,9 @@ constexpr const E&& error() const&& noexcept; constexpr E&& error() && noexcept; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) const&; - template - constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; + template constexpr @\exposid{error-value-type}@ error_or(G&& def) &&; template constexpr void value_or(U&&) const = delete; @@ -183,7 +175,6 @@ \pnum If \tcode{E} is not an lvalue reference type, \tcode{E} shall meet the requirements of \oldconcept{Destructible} (\tref{cpp17.destructible}). - \rSec3[expected.void.cons]{Constructors} \indexlibraryctor{expected}% @@ -199,11 +190,10 @@ \indexlibraryctor{expected}% \begin{itemdecl} -constexpr expected(const expected& rhs) noexcept(is_nothrow_copy_constructible_v); +constexpr expected(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{rhs.error()}. @@ -233,7 +223,7 @@ \pnum \effects -If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.error())}. +If \tcode{rhs.has_value()} is \tcode{false}, direct-non-list-initializes \exposid{unex} with \tcode{std::move(rhs.\exposid{unex})}. \pnum \ensures @@ -484,12 +474,10 @@ \indexlibrarymember{operator=}{expected}% \begin{itemdecl} -constexpr expected& operator=(const expected& rhs) noexcept(is_nothrow_copy_constructible_v && - is_nothrow_copy_assignable_v); +constexpr expected& operator=(const expected& rhs); \end{itemdecl} \begin{itemdescr} - \pnum \effects If \tcode{this->has_value() \&\& rhs.has_value()}, no effects. Otherwise, if \tcode{this->has_value()}, equivalent to: \tcode{construct_at(addressof(\exposid{unex}), rhs.\exposid{unex}); \exposid{has_val} = false;} Otherwise, if \tcode{rhs.has_value()}, destroys \exposid{unex} and sets \exposid{has_val} to \tcode{true}. Otherwise, equivalent to \tcode{\exposid{unex} = rhs.\exposid{unex}}. @@ -682,7 +670,7 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \throws @@ -697,11 +685,11 @@ \begin{itemdescr} \pnum \mandates -\tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_move_constructible_v} is \tcode{true}. +\tcode{is_copy_constructible_v<\exposid{error-value-type}>} is \tcode{true} and \tcode{is_move_constructible_v<\exposid{error-value-type}>} is \tcode{true}. \pnum \throws -\tcode{bad_expected_access(std::move(error()))} if \tcode{has_value()} is \tcode{false}. +\tcode{bad_expected_access(std::move(\exposid{unex}).error())} if \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{error}{expected}% @@ -773,7 +761,7 @@ \pnum \returns -\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(error())} otherwise. +\tcode{std::forward(def)} if \tcode{has_value()} is \tcode{true}, \tcode{std::move(\exposid{unex}).error()} otherwise. \end{itemdescr} \indexlibrarymember{value_or}{expected}% @@ -803,17 +791,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, error());} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, error());} \end{itemdescr} \indexlibrarymember{and_then}{expected}% @@ -825,17 +816,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cvref_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{error_type} is the same type as \tcode{E}. +\tcode{U} is a specialization of \tcode{expected} and \tcode{is_same_v} is \tcode{true}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, std::move(error()));} where \tcode{U} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return invoke(std::forward(f)); else return U(unexpect, std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -847,13 +841,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. +\tcode{G} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), error());} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), error());} \end{itemdescr} \indexlibrarymember{or_else}{expected}% @@ -865,13 +862,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cvref_t>}. + \pnum \mandates -\tcode{remove_cvref_t>} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. +\tcode{G} is a specialization of \tcode{expected} and its \tcode{value_type} is the same type as \tcode{T}. \pnum \effects -Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), std::move(error()));} where \tcode{G} is \tcode{remove_cvref_t>}. +Equivalent to: \tcode{if (has_value()) return G(); else return invoke(std::forward(f), std::move(\exposid{unex}).error());} \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -883,17 +883,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints \tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{U} is a valid value type for \tcode{expected}, where \tcode{U} is \tcode{remove_cv_t>}. +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f)));} is well-formed. \pnum \effects -If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}; if \tcode{U} is an lvalue reference type, \tcode{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform}{expected}% @@ -905,17 +908,20 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{U} be \tcode{remove_cv_t>}. + \pnum \constraints -\tcode{is_constructible_v} is \tcode{true}. +\tcode{is_constructible_v} is \tcode{true}. \pnum \mandates -\tcode{U} is a valid value type for \tcode{expected}, where \tcode{U} is \tcode{remove_cv_t>}. +\tcode{U} is a valid value type for \tcode{expected}. If \tcode{is_void_v} is \tcode{false}, the declaration \tcode{U u(invoke(std::forward(f)));} is well-formed. \pnum \effects -If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(error()))}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. +If \tcode{has_value()} is \tcode{false}, returns \tcode{expected(unexpect, std::move(\exposid{unex}).error())}. Otherwise, if \tcode{is_void_v} is \tcode{false}, returns an \tcode{expected} object whose \exposid{has_val} member is \tcode{true} and \tcode{val} member is direct-non-list-initialized with \tcode{invoke(std::forward(f))}; if \tcode{U} is an lvalue reference type, \tcode{val} instead points to the object to which the result of that invocation refers. Otherwise, evaluates \tcode{invoke(std::forward(f))} and then returns \tcode{expected()}. \end{itemdescr} \indexlibrarymember{transform_error}{expected}% @@ -927,9 +933,12 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates -\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), error()));} is well-formed, where \tcode{G} is \tcode{remove_cv_t>}. +\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), error()));} is well-formed. \pnum \returns @@ -945,13 +954,16 @@ \end{itemdecl} \begin{itemdescr} +\pnum +Let \tcode{G} be \tcode{remove_cv_t>}. + \pnum \mandates -\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), std::move(error())));} is well-formed, where \tcode{G} is \tcode{remove_cv_t>}. +\tcode{G} is a valid template argument for \tcode{unexpected} and the declaration \tcode{G g(invoke(std::forward(f), std::move(\exposid{unex}).error()));} is well-formed. \pnum \returns -If \tcode{has_value()} is \tcode{true}, \tcode{expected()}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(error()))}. +If \tcode{has_value()} is \tcode{true}, \tcode{expected()}; otherwise, an \tcode{expected} object whose \exposid{has_val} member is \tcode{false} and \exposid{unex} member is direct-non-list-initialized with \tcode{invoke(std::forward(f), std::move(\exposid{unex}).error())}. \end{itemdescr} \rSec3[expected.void.eq]{Equality operators} diff --git a/tests/beman/expected/CMakeLists.txt b/tests/beman/expected/CMakeLists.txt index f0ae0045..7bdae574 100644 --- a/tests/beman/expected/CMakeLists.txt +++ b/tests/beman/expected/CMakeLists.txt @@ -219,8 +219,8 @@ beman_add_xfail_compile_test( LINK_LIBRARIES beman::expected ) beman_add_xfail_compile_test( - transform_error_ref_result_fail - SOURCE transform_error_ref_result_fail.test.cpp + transform_error_rvalue_ref_result_fail + SOURCE transform_error_rvalue_ref_result_fail.test.cpp LINK_LIBRARIES beman::expected ) @@ -370,8 +370,8 @@ beman_add_xfail_compile_test( LINK_LIBRARIES beman::expected ) beman_add_xfail_compile_test( - expected_ref_transform_error_ref_result_fail - SOURCE expected_ref_transform_error_ref_result_fail.test.cpp + expected_ref_transform_error_rvalue_ref_result_fail + SOURCE expected_ref_transform_error_rvalue_ref_result_fail.test.cpp LINK_LIBRARIES beman::expected ) @@ -419,8 +419,8 @@ beman_add_xfail_compile_test( LINK_LIBRARIES beman::expected ) beman_add_xfail_compile_test( - expected_ref_e_transform_error_ref_result_fail - SOURCE expected_ref_e_transform_error_ref_result_fail.test.cpp + expected_ref_e_transform_error_rvalue_ref_result_fail + SOURCE expected_ref_e_transform_error_rvalue_ref_result_fail.test.cpp LINK_LIBRARIES beman::expected ) @@ -468,8 +468,8 @@ beman_add_xfail_compile_test( LINK_LIBRARIES beman::expected ) beman_add_xfail_compile_test( - expected_ref_both_transform_error_ref_result_fail - SOURCE expected_ref_both_transform_error_ref_result_fail.test.cpp + expected_ref_both_transform_error_rvalue_ref_result_fail + SOURCE expected_ref_both_transform_error_rvalue_ref_result_fail.test.cpp LINK_LIBRARIES beman::expected ) diff --git a/tests/beman/expected/expected_ref_both.test.cpp b/tests/beman/expected/expected_ref_both.test.cpp index eb1683c4..dbf1cc7d 100644 --- a/tests/beman/expected/expected_ref_both.test.cpp +++ b/tests/beman/expected/expected_ref_both.test.cpp @@ -46,7 +46,7 @@ TEST_CASE("expected: special member availability", "[expected_ref_both]") CHECK(std::is_move_constructible_v>); } -TEST_CASE("expected: fully trivial — both sides are pointers", "[expected_ref_both]") { +TEST_CASE("expected: fully trivial - both sides are pointers", "[expected_ref_both]") { // Trivially copyable/movable/destructible CHECK(std::is_trivially_copy_constructible_v>); CHECK(std::is_trivially_move_constructible_v>); diff --git a/tests/beman/expected/expected_ref_both_initializer_list_fail.test.cpp b/tests/beman/expected/expected_ref_both_initializer_list_fail.test.cpp index 96c1e212..fde1ce6f 100644 --- a/tests/beman/expected/expected_ref_both_initializer_list_fail.test.cpp +++ b/tests/beman/expected/expected_ref_both_initializer_list_fail.test.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // NEGATIVE: expected initializer-list error construction: E& cannot bind to the list // EXPECT: "initializer-list error construction cannot bind a reference|use of deleted function|call to deleted" +// EXPECT: "attempting to reference" #include using namespace beman::expected; diff --git a/tests/beman/expected/expected_ref_both_transform_error_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_both_transform_error_ref_result_fail.test.cpp deleted file mode 100644 index d2832629..00000000 --- a/tests/beman/expected/expected_ref_both_transform_error_ref_result_fail.test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// NEGATIVE: transform_error on expected must return object type G, not reference -// EXPECT: "G must be an object type" -#include -void test() { - int err = 0; - beman::expected::expected e(beman::expected::unexpect, err); - // G = int& (reference type, not object type) - e.transform_error([](int& v) -> int& { return v; }); -} diff --git a/tests/beman/expected/expected_ref_both_transform_error_rvalue_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_both_transform_error_rvalue_ref_result_fail.test.cpp new file mode 100644 index 00000000..a2085e6e --- /dev/null +++ b/tests/beman/expected/expected_ref_both_transform_error_rvalue_ref_result_fail.test.cpp @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// NEGATIVE: transform_error on expected Mandates that G is a valid argument for +// unexpected. An lvalue reference is one; an rvalue reference is not. +// EXPECT: "G must be an object type or an lvalue reference to one" +#include +#include +void test() { + static int s = 0; + int err = 0; + beman::expected::expected e(beman::expected::unexpect, err); + // G = int&& + e.transform_error([](int&) -> int&& { return std::move(s); }); +} diff --git a/tests/beman/expected/expected_ref_e_initializer_list_fail.test.cpp b/tests/beman/expected/expected_ref_e_initializer_list_fail.test.cpp index f9707a40..66c4c54a 100644 --- a/tests/beman/expected/expected_ref_e_initializer_list_fail.test.cpp +++ b/tests/beman/expected/expected_ref_e_initializer_list_fail.test.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // NEGATIVE: expected initializer-list error construction: E& cannot bind to the list // EXPECT: "initializer-list error construction cannot bind a reference|use of deleted function|call to deleted" +// EXPECT: "attempting to reference" #include using namespace beman::expected; diff --git a/tests/beman/expected/expected_ref_e_transform_error_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_e_transform_error_ref_result_fail.test.cpp deleted file mode 100644 index deba573e..00000000 --- a/tests/beman/expected/expected_ref_e_transform_error_ref_result_fail.test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// NEGATIVE: transform_error on expected must return object type G, not reference -// EXPECT: "G must be an object type" -#include -void test() { - int err = 0; - beman::expected::expected e(beman::expected::unexpect, err); - // G = int& (reference type, not object type) - e.transform_error([](int& v) -> int& { return v; }); -} diff --git a/tests/beman/expected/expected_ref_e_transform_error_rvalue_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_e_transform_error_rvalue_ref_result_fail.test.cpp new file mode 100644 index 00000000..f779110f --- /dev/null +++ b/tests/beman/expected/expected_ref_e_transform_error_rvalue_ref_result_fail.test.cpp @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// NEGATIVE: transform_error on expected Mandates that G is a valid argument for +// unexpected. An lvalue reference is one; an rvalue reference is not. +// EXPECT: "G must be an object type or an lvalue reference to one" +#include +#include +void test() { + static int s = 0; + int err = 0; + beman::expected::expected e(beman::expected::unexpect, err); + // G = int&& + e.transform_error([](int&) -> int&& { return std::move(s); }); +} diff --git a/tests/beman/expected/expected_ref_transform_error_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_transform_error_ref_result_fail.test.cpp deleted file mode 100644 index 290a389f..00000000 --- a/tests/beman/expected/expected_ref_transform_error_ref_result_fail.test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// NEGATIVE: transform_error on expected must return object type G, not reference -// EXPECT: "G must be an object type" -#include -void test() { - int x = 0; - beman::expected::expected e(x); - // G = int& (reference type, not object type) - e.transform_error([](int& v) -> int& { return v; }); -} diff --git a/tests/beman/expected/expected_ref_transform_error_rvalue_ref_result_fail.test.cpp b/tests/beman/expected/expected_ref_transform_error_rvalue_ref_result_fail.test.cpp new file mode 100644 index 00000000..3ccd3cbf --- /dev/null +++ b/tests/beman/expected/expected_ref_transform_error_rvalue_ref_result_fail.test.cpp @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// NEGATIVE: transform_error on expected Mandates that G is a valid argument for +// unexpected. An lvalue reference is one; an rvalue reference is not. +// EXPECT: "G must be an object type or an lvalue reference to one" +#include +#include +void test() { + static int s = 0; + int x = 0; + beman::expected::expected e(x); + // G = int&& + e.transform_error([](int&) -> int&& { return std::move(s); }); +} diff --git a/tests/beman/expected/expected_review_corrections.test.cpp b/tests/beman/expected/expected_review_corrections.test.cpp index 8c55b343..c03ca897 100644 --- a/tests/beman/expected/expected_review_corrections.test.cpp +++ b/tests/beman/expected/expected_review_corrections.test.cpp @@ -382,3 +382,270 @@ TEST_CASE("converting from an rvalue value expected still moves", "[convert][mov REQUIRE(b.error() == "baz"); } } + +// ============================================================================= +// The same guarantee for the rvalue observers and monadic operations: an rvalue +// expected with a reference error moves the holder, std::move(unex).error(), and +// never the referent, which it does not own. Each case is paired with the +// value-E behavior, where the error is owned and is moved. +// ============================================================================= + +TEST_CASE("rvalue error_or does not steal a referenced error", "[ref_e][observers][shallow]") { + SECTION("expected") { + std::string s = "bar"; + expected e{unexpect, s}; + std::string got = std::move(e).error_or(std::string("x")); + REQUIRE(s == "bar"); // copied, not moved + REQUIRE(got == "bar"); + } + SECTION("expected") { + std::string s = "bar"; + expected e{unexpect, s}; + std::string got = std::move(e).error_or(std::string("x")); + REQUIRE(s == "bar"); + REQUIRE(got == "bar"); + } + SECTION("expected") { + std::string s = "bar"; + expected e{unexpect, s}; + std::string got = std::move(e).error_or(std::string("x")); + REQUIRE(s == "bar"); + REQUIRE(got == "bar"); + } + SECTION("an owned error is still moved") { + expected e{unexpect, "baz"}; + std::string got = std::move(e).error_or(std::string("x")); + REQUIRE(e.error().empty()); + REQUIRE(got == "baz"); + } +} + +TEST_CASE("rvalue value() does not steal a referenced error into the exception", "[ref_e][observers][shallow]") { + SECTION("expected") { + std::string s = "bar"; + expected e{unexpect, s}; + try { + (void)std::move(e).value(); + FAIL("value() must throw"); + } catch (const bad_expected_access& ex) { + REQUIRE(ex.error() == "bar"); + } + REQUIRE(s == "bar"); + } + SECTION("expected") { + std::string s = "bar"; + expected e{unexpect, s}; + try { + std::move(e).value(); + FAIL("value() must throw"); + } catch (const bad_expected_access& ex) { + REQUIRE(ex.error() == "bar"); + } + REQUIRE(s == "bar"); + } +} + +TEST_CASE("rvalue or_else and transform_error pass a referenced error as an lvalue", "[ref_e][monadic][shallow]") { + SECTION("or_else, expected") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).or_else([](auto&& x) -> expected { + static_assert(std::is_same_v); + return expected(unexpect, x); + }); + REQUIRE(s == "bar"); + REQUIRE(&r.error() == &s); + } + SECTION("or_else, const rvalue") { + std::string s = "bar"; + const expected e{unexpect, s}; + auto r = std::move(e).or_else([](auto&& x) -> expected { + static_assert(std::is_same_v); + return expected(unexpect, x); + }); + REQUIRE(s == "bar"); + REQUIRE(&r.error() == &s); + } + SECTION("or_else, expected") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).or_else([](auto&& x) -> expected { + static_assert(std::is_same_v); + return expected(unexpect, x); + }); + REQUIRE(s == "bar"); + REQUIRE(&r.error() == &s); + } + SECTION("transform_error, a callable that would move from an rvalue") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).transform_error([](auto&& x) { + static_assert(std::is_same_v); + return std::string(std::forward(x)); + }); + REQUIRE(s == "bar"); + REQUIRE(r.error() == "bar"); + } + SECTION("transform_error, expected") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).transform_error([](auto&& x) { + static_assert(std::is_same_v); + return std::string(std::forward(x)); + }); + REQUIRE(s == "bar"); + REQUIRE(r.error() == "bar"); + } + SECTION("an owned error is still passed as an rvalue") { + expected e{unexpect, "baz"}; + auto r = std::move(e).transform_error([](auto&& x) { + static_assert(std::is_same_v); + return std::string(std::forward(x)); + }); + REQUIRE(e.error().empty()); + REQUIRE(r.error() == "baz"); + } +} + +TEST_CASE("rvalue and_then and transform rebind a referenced error", "[ref_e][monadic][shallow]") { + SECTION("and_then") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).and_then([](int&&) { return expected(1L); }); + REQUIRE(s == "bar"); + REQUIRE(&r.error() == &s); + } + SECTION("transform") { + std::string s = "bar"; + expected e{unexpect, s}; + auto r = std::move(e).transform([](int&&) { return 1L; }); + REQUIRE(s == "bar"); + REQUIRE(&r.error() == &s); + } +} + +// ============================================================================= +// A deleted constructor template that takes a forwarding reference deduces the +// class itself from a const rvalue of the same type. Its constraint then asked +// whether a temporary of the referent type could be made from the class, which +// under gcc depends on itself: copying from a const rvalue was a hard error and +// not a copy. The deleted overloads now carry the enabled overload's guards. +// ============================================================================= + +static_assert(std::is_constructible_v, const expected&&>); +static_assert(std::is_constructible_v, const expected&&>); +static_assert( + std::is_constructible_v, const expected&&>); +static_assert(std::is_constructible_v, const unexpected&&>); +// The guard does not reopen what the overload is there to delete. +static_assert(!std::is_constructible_v, std::string>); +static_assert(!std::is_constructible_v, const char*>); +static_assert(!std::is_constructible_v, std::string>); + +TEST_CASE("copying a reference expected from a const rvalue of its own type rebinds", "[ref][cons][shallow]") { + SECTION("expected") { + std::string s = "bar"; + const expected c{s}; + expected d{std::move(c)}; + REQUIRE(&*d == &s); + REQUIRE(s == "bar"); + } + SECTION("unexpected") { + std::string s = "bar"; + const unexpected c{s}; + unexpected d{std::move(c)}; + REQUIRE(&d.error() == &s); + REQUIRE(s == "bar"); + } + SECTION("expected, through its unexpected") { + std::string s = "bar"; + const expected c{unexpect, s}; + expected d{std::move(c)}; + REQUIRE(&d.error() == &s); + REQUIRE(s == "bar"); + } +} + +// ============================================================================= +// transform and transform_error leave the callable's result type a reference +// when it is one: remove_cv_t does not strip it. As optional::transform +// yields an optional, a callable that returns an lvalue reference yields an +// expected that refers to the same object. An rvalue reference is rejected. +// ============================================================================= + +TEST_CASE("transform with a callable returning an lvalue reference yields expected", "[monadic][ref]") { + std::string target = "target"; + SECTION("primary") { + expected e{1}; + auto r = e.transform([&](int&) -> std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&*r == &target); + } + SECTION("primary, const result") { + expected e{1}; + auto r = std::move(e).transform([&](int&&) -> const std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&*r == &target); + } + SECTION("void") { + expected e; + auto r = e.transform([&]() -> std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&*r == &target); + } + SECTION("reference specialization") { + expected e{target}; + auto r = e.transform([](std::string& s) -> std::string& { return s; }); + static_assert(std::is_same_v>); + REQUIRE(&*r == &target); + } + SECTION("the error passes through") { + expected e{unexpect, 7}; + auto r = e.transform([&](int&) -> std::string& { return target; }); + REQUIRE(!r.has_value()); + REQUIRE(r.error() == 7); + } +} + +TEST_CASE("transform_error with a callable returning an lvalue reference yields expected", "[monadic][ref_e]") { + std::string target = "target"; + SECTION("primary") { + expected e{unexpect, 1}; + auto r = e.transform_error([&](int&) -> std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&r.error() == &target); + } + SECTION("primary, const result from an rvalue") { + expected e{unexpect, 1}; + auto r = std::move(e).transform_error([&](int&&) -> const std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&r.error() == &target); + } + SECTION("void") { + expected e{unexpect, 1}; + auto r = e.transform_error([&](int&) -> std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&r.error() == &target); + } + SECTION("reference specialization") { + int v = 3; + expected e{unexpect, 1}; + auto r = e.transform_error([&](int&) -> std::string& { return target; }); + static_assert(std::is_same_v>); + REQUIRE(&r.error() == &target); + expected h{v}; + auto q = h.transform_error([&](int&) -> std::string& { return target; }); + REQUIRE(&*q == &v); + } + SECTION("a reference error, passed through unchanged") { + expected e{unexpect, target}; + auto r = e.transform_error([](std::string& s) -> std::string& { return s; }); + static_assert(std::is_same_v>); + REQUIRE(&r.error() == &target); + } + SECTION("the value passes through") { + expected e{5}; + auto r = e.transform_error([&](int&) -> std::string& { return target; }); + REQUIRE(*r == 5); + } +} diff --git a/tests/beman/expected/expected_void_ref_e_initializer_list_fail.test.cpp b/tests/beman/expected/expected_void_ref_e_initializer_list_fail.test.cpp index 57070f18..d9ea55e5 100644 --- a/tests/beman/expected/expected_void_ref_e_initializer_list_fail.test.cpp +++ b/tests/beman/expected/expected_void_ref_e_initializer_list_fail.test.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // NEGATIVE: expected initializer-list error construction: E& cannot bind to the list // EXPECT: "initializer-list error construction cannot bind a reference|use of deleted function|call to deleted" +// EXPECT: "attempting to reference" #include using namespace beman::expected; diff --git a/tests/beman/expected/transform_error_ref_result_fail.test.cpp b/tests/beman/expected/transform_error_ref_result_fail.test.cpp deleted file mode 100644 index 5e5d8781..00000000 --- a/tests/beman/expected/transform_error_ref_result_fail.test.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// NEGATIVE: transform_error Mandates G is valid for unexpected (not a reference) -// EXPECT: "G must be an object type" -#include -void test() { - beman::expected::expected e(beman::expected::unexpect, 1); - e.transform_error([](int) -> int& { - static int x = 0; - return x; - }); -} diff --git a/tests/beman/expected/transform_error_rvalue_ref_result_fail.test.cpp b/tests/beman/expected/transform_error_rvalue_ref_result_fail.test.cpp new file mode 100644 index 00000000..ff82190f --- /dev/null +++ b/tests/beman/expected/transform_error_rvalue_ref_result_fail.test.cpp @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// NEGATIVE: transform_error on expected Mandates that G is a valid argument for +// unexpected. An lvalue reference is one; an rvalue reference is not. +// EXPECT: "G must be an object type or an lvalue reference to one" +#include +#include +void test() { + static int s = 0; + beman::expected::expected e(beman::expected::unexpect, 1); + // G = int&& + e.transform_error([](int) -> int&& { return std::move(s); }); +}