Skip to content

fix(debug): resolve ambiguous assert() for lambdas on AppleClang - #32

Closed
igagis-bot wants to merge 1 commit into
cppfw:mainfrom
igagis-bot:fix/assert-lambda-ambiguity
Closed

igagis-bot wants to merge 1 commit into
cppfw:mainfrom
igagis-bot:fix/assert-lambda-ambiguity

Conversation

@igagis-bot

Copy link
Copy Markdown
Contributor

Problem

The iOS CI build fails to compile assert(lambda, SL) calls with:

error: call to 'assert' is ambiguous
  note: candidate function  (assert(bool, const source_location&))
  note: candidate function  (assert(const std::function<bool()>&, const source_location&))

AppleClang (unlike GCC/Clang/MSVC) considers the bool overload a viable candidate for a
capturing lambda argument (it treats the lambda as convertible to bool), so the call to
assert() between the bool and the std::function<bool()> overloads becomes ambiguous.

The assert(lambda, SL) lazy-condition pattern is used in ~116 call sites across many
libraries, so the failure surfaces in whichever translation unit is compiled first (e.g.
rasterimage/image_variant.hpp).

Fix

Add a constrained template overload that matches callables whose result converts to bool.
It is an exact match, so it is preferred over the bool overload on every compiler:

template <
    typename condition_checker_type,
    typename = std::enable_if_t<
        std::is_invocable_v<condition_checker_type>
        && std::is_convertible_v<std::invoke_result_t<condition_checker_type>, bool>
        && !std::is_pointer_v<std::remove_reference_t<condition_checker_type>>>>
inline void assert(condition_checker_type&&, const utki::source_location& = ...);

The constraint:

  • keeps pointers (raw / shared_ptr / unique_ptr / function pointers) on their existing overloads;
  • keeps std::function<void()> (and other non-bool callables) on the existing std::function<func_type> overload;
  • the callable is invoked only in DEBUG builds, preserving the existing no-op behavior in release builds.

Verification

  • Full Linux dbg build of all (g++ 14): 138 targets, 0 errors.
  • utki-tests: 327/327 pass.
  • svgdom-tests: 51/51 pass (exercises 48 assert(lambda, SL) sites at runtime).
  • A focused overload-resolution test compiles cleanly under both g++ and clang++.

Passing a capturing lambda to utki::assert() is ambiguous on AppleClang: it
considers both the assert(bool, source_location) and the
assert(const std::function<bool()>&, source_location) overloads viable
(it treats the lambda as convertible to bool), so overload resolution fails.
GCC/Clang/MSVC do not consider the bool overload viable for a lambda, so the
ambiguity only surfaces on AppleClang (iOS).

Add a constrained template overload that matches callables whose result
converts to bool. It is an exact match, so it is preferred over the bool
overload on every compiler. It excludes pointers (which have dedicated
overloads) and non-bool-convertible callables such as std::function<void()>.
The callable is invoked only in DEBUG builds to preserve the existing
no-op behavior in release builds.
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.85%. Comparing base (def482f) to head (05b17e0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #32      +/-   ##
==========================================
- Coverage   84.86%   84.85%   -0.01%     
==========================================
  Files          31       31              
  Lines        1559     1558       -1     
  Branches      384      384              
==========================================
- Hits         1323     1322       -1     
  Misses        174      174              
  Partials       62       62              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@igagis-bot

Copy link
Copy Markdown
Contributor Author

Superseded by #33: this PR only added a 2-arg (condition_lambda, SL) template overload, but the codebase also has 3-arg assert(condition, print, SL) call shapes where the same ObjC++ lambda-to-bool conversion makes the bool and std::function<bool()> overloads ambiguous. #33 constrains all three bool-taking overloads to exactly bool via SFINAE, covering every arity.

@igagis-bot igagis-bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants