Skip to content

Add generic value constructor to std/type/any#1214

Merged
marcauberer merged 2 commits into
mainfrom
std/any-value-constructor
Jun 15, 2026
Merged

Add generic value constructor to std/type/any#1214
marcauberer merged 2 commits into
mainfrom
std/any-value-constructor

Conversation

@marcauberer

Copy link
Copy Markdown
Member

What changed

  • Added a generic value constructor Any.ctor<T>(const T& value) to std/type/any, so an Any can be constructed directly from a value (e.g. Any(42)), alongside the existing any() factory and set().
  • Extended the any-smoke test to cover the value constructor on the inline (Any(42)) and heap (Any(small)) paths.
  • Extended the any-nontrivial test to cover the value constructor on a non-trivial payload, so the lifetime counters verify it deep-copies via the copy ctor and is destroyed correctly.

Why

The value constructor is a natural, convenient way to box a value and matches std::any's usage. Constructing from another Any must still pick the dedicated copy constructor rather than the generic value constructor.

How it was validated

  • cmake --build cmake-build-debug --target spice spicetest — builds clean
  • cmake-build-debug/test/spicetest --gtest_filter='StdTests.type_any*'type_anySmoke and type_anyNontrivial pass

Follow-up / known limitations

marcauberer and others added 2 commits June 15, 2026 22:16
When an explicitly declared (non-generic) function and a generic
substitution match a call equally well, overload resolution reported an
ambiguity. Mirror C++ semantics and prefer the non-template, applied as a
secondary tie-break after qualifier specificity. A losing generic
substitution that was only inserted for this match is de-registered again,
so the IR generator never emits a manifestation that was never type-checked.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Allow constructing an Any directly from a value, e.g. Any(42), in addition
to the any() factory and set(). Extend the any-smoke and any-nontrivial
tests to cover the new constructor on the inline, heap and non-trivial
(copy-ctor/dtor tracked) paths.

This relies on overload resolution preferring the dedicated copy
constructor over the generic value constructor when copying an Any.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@marcauberer marcauberer requested review from a team as code owners June 15, 2026 20:17
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Base automatically changed from fix/prefer-explicit-overload-over-generic to main June 15, 2026 20:48
@pull-request-size pull-request-size Bot added size/M and removed size/S labels Jun 15, 2026
@marcauberer marcauberer added this to the 0.27.0 milestone Jun 15, 2026
@marcauberer marcauberer merged commit 818c417 into main Jun 15, 2026
6 of 7 checks passed
@marcauberer marcauberer deleted the std/any-value-constructor branch June 15, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant