Skip to content

Provide case_() as a domain-specific alternative to switch_expression::append() #51

Description

@ipapadop

Motivation

switch_expression::append() expands a switch with additional case expressions:

auto expanded = ex.append(deferred::case_(11, body));

The generic name append does not describe what may be appended. A member named case_ would make the expression read more like the C++ construct it models:

auto expanded = ex.case_(11, body);

Proposed API

Consider either accepting a label and body directly:

template<typename Label, typename Body>
auto case_(Label&& label, Body&& body) const&;

template<typename Label, typename Body>
auto case_(Label&& label, Body&& body) &&;

or accepting already-constructed case expressions:

auto expanded = ex.case_(deferred::case_(11, body));

The direct label/body form is more concise and avoids the visually repetitive .case_(deferred::case_(...)).

Design considerations

  • Existing cases must retain precedence over newly added cases.
  • The operation should return a new flattened switch_expression, matching current append() value semantics.
  • The const& overload should copy owned state; the && overload should move it.
  • Move-only labels and bodies must work through the rvalue overload.
  • Result types must be recomputed across the default, existing cases, and added case.
  • Decide whether append() remains as an alias, is deprecated, or is eventually replaced.
  • The overload must not introduce ambiguity with the namespace-level deferred::case_() factory.

Compatibility

Adding case_() alongside append() is backward compatible. Removing or renaming append() should be deferred to a major release.

Acceptance criteria

  • A switch can be expanded using member case_().
  • Existing-case, added-case, and default behavior are covered.
  • Duplicate labels preserve existing-case precedence.
  • Heterogeneous results are deduced correctly.
  • Const-lvalue and move-only rvalue expansion are tested.
  • The relationship between case_() and append() is documented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions