Skip to content

feat: add function object support#1163

Merged
gennaroprota merged 2 commits intocppalliance:developfrom
gennaroprota:feat/function_object_support
Mar 16, 2026
Merged

feat: add function object support#1163
gennaroprota merged 2 commits intocppalliance:developfrom
gennaroprota:feat/function_object_support

Conversation

@gennaroprota
Copy link
Copy Markdown
Collaborator

@gennaroprota gennaroprota commented Mar 3, 2026

Edit: This description is now obsolete. Please refer to the commit messages for up to date info.

This PR adds detection of constexpr variables whose type is a record providing only operator() overloads, and documents them as callable objects in the Functions tranche. The type of the variable is hidden as implementation-defined, and breadcrumbs render in f() style for readability, but operator() signatures remain fully visible in the variable synopsis and each overload gets its own detail page---preserving the fact that the callable is a function object, not an ordinary function.

Detection triggers:

  • Auto-detection (on by default, controlled by auto-function-objects) for records whose only public non-special members are operator() overloads, including class templates when the variable lives in an enclosing scope of the type.

  • The @function_object documentation command for cases that fail auto-detection (e.g. types with extra members).

Key implementation details:

  • FunctionObjectFinalizer runs before OverloadsFinalizer: hides the type of the variable, restores operator() to Regular extraction, re-parents overloads under the variable, and appends a fixed disclaimer note.

  • VariableSymbol gains FunctionObjectOverloads and satisfies the SymbolParent concept, so MultiPageVisitor generates detail pages for each operator() overload.

  • Handlebars templates render linked operator() signatures in the variable synopsis with "» more..." links to the detail pages.

Closes #564.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 3, 2026

🚧 Danger.js checks for MrDocs are experimental; expect some rough edges while we tune the rules.

✨ Highlights

  • 🧪 Existing golden tests changed (behavior likely shifted)

🧾 Changes by Scope

Scope Lines Δ Lines + Lines - Files Δ Files + Files ~ Files ↔ Files -
🥇 Golden Tests 9289 9202 87 20 17 3 - -
🛠️ Source 897 820 77 16 3 13 - -
🧪 Unit Tests 507 507 - 2 2 - - -
📄 Docs 16 16 - 2 - 2 - -
🏗️ Build / Toolchain 3 2 1 1 - 1 - -
Total 10712 10547 165 41 22 19 - -

Legend: Files + (added), Files ~ (modified), Files ↔ (renamed), Files - (removed)

🔝 Top Files

  • test-files/golden-tests/symbols/variable/function-objects.xml (Golden Tests): 4393 lines Δ (+4393 / -0)
  • test-files/golden-tests/symbols/variable/function-objects.html (Golden Tests): 1475 lines Δ (+1475 / -0)
  • test-files/golden-tests/symbols/variable/function-objects.adoc (Golden Tests): 1195 lines Δ (+1195 / -0)

Generated by 🚫 dangerJS against a347aa4

@cppalliance-bot
Copy link
Copy Markdown

cppalliance-bot commented Mar 3, 2026

An automated preview of the documentation is available at https://1163.mrdocs.prtest2.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-03-13 17:32:42 UTC

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 90.41394% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.44%. Comparing base (2a75692) to head (a347aa4).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
...ib/Metadata/Finalizers/FunctionObjectFinalizer.cpp 77.91% 7 Missing and 29 partials ⚠️
src/lib/Metadata/Symbol/Function.cpp 94.11% 0 Missing and 3 partials ⚠️
include/mrdocs/Metadata/Symbol/SymbolID.hpp 50.00% 1 Missing and 1 partial ⚠️
src/lib/AST/ExtractDocComment.cpp 80.00% 0 Missing and 2 partials ⚠️
include/mrdocs/Metadata/DocComment.hpp 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1163      +/-   ##
===========================================
+ Coverage    77.14%   77.44%   +0.30%     
===========================================
  Files          310      313       +3     
  Lines        29043    29475     +432     
  Branches      5795     5880      +85     
===========================================
+ Hits         22404    22827     +423     
+ Misses        4420     4403      -17     
- Partials      2219     2245      +26     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gennaroprota gennaroprota force-pushed the feat/function_object_support branch from 19d7451 to e9b70af Compare March 3, 2026 15:56
@alandefreitas
Copy link
Copy Markdown
Collaborator

alandefreitas commented Mar 3, 2026

How does this solution relate to the proposals in #564? 🙂

(btw, #564 is from Apr 10, 2024 - it's an investigation that took a long time - not 2026 AI slop 😂)

Edit: nevermind. I think I already got it. :)

Copy link
Copy Markdown
Collaborator

@alandefreitas alandefreitas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't "functors" formal enough to be used here? It would be a much shorter command and a more concise way to describe it over and over again without needing a "_".

The fixtures should go to better directories, though. Maybe the directories need better documentation, but you can see they shouldn't go there by comparing with the siblings (and you'll see it's definitely not "core", which is used for really core functionality that's not optional). Symbol types and their features are tested in the symbols directory for each symbol. And since there's an option involved, every new option should be tested in the options directory. Then we have new features for variables that should be tested. Then we have a new field in variables, and that's not reflected there. Then we have a folder for javadoc commands that should be tested, and we need a test there because we introduced a new command.

The test cases in symbols/variable should also explore what happens when there is non-conflicting type information, but the variable is forced to be treated as a function object anyway. There are real-world patterns like this. Another pattern we see very often is the combination of template parameters and arguments: impl function with template, different variables with the same function but different params, and so on.

In the existing test, I'm not sure I understand the mechanism through which it goes to the list of functions in the interface. I'm not sure if cppalliance does that or shows it as a separate kind of entity. Because they do behave differently. All niebloids also have a special banner describing how that function works differently. Another thing to explore is how all of that works when one is visible, and the other is an implementation detail, and so on. Another thing to test and design is what happens when documentation goes on the other symbol.

In the output of the existing test, all functors look wrong. They look nothing like niebloids in cppreference. Many of them look arbitrary and not like valid C++ syntax at all. For instance, it tends to look like:

bool single_overload();

(not very different from an overload set with the main function name replaced from operator() to the variable name and with const removed).

rather than:

constexpr /* implementation-defined */ single_overload = {};

bool
operator()() const;

One more thing missing is the presentation of this feature on the landing page. The snippets on the landing page are meant to showcase features that only Mrdocs can support, and this is definitely one of them.

Good work here overall, though. This is a big design decision.

Comment thread include/mrdocs/Metadata/Symbol/Variable.hpp Outdated
Comment thread share/mrdocs/addons/generator/common/partials/symbol/qualified-name-text.hbs Outdated
Comment thread include/mrdocs/Metadata/Symbol/Variable.hpp Outdated
Comment thread include/mrdocs/Metadata/Symbol/Variable.hpp Outdated
Comment thread share/mrdocs/addons/generator/common/partials/symbol/qualified-name-title.hbs Outdated
Comment thread src/lib/Metadata/Finalizers/FunctionObjectFinalizer.cpp Outdated
Comment thread src/lib/Metadata/Finalizers/FunctionObjectFinalizer.cpp
Comment thread src/lib/Metadata/Finalizers/FunctionObjectFinalizer.cpp
Comment thread src/lib/Metadata/Symbol/Variable.cpp
Comment thread src/lib/Support/Reflection/Reflection.hpp Outdated
@gennaroprota gennaroprota force-pushed the feat/function_object_support branch 12 times, most recently from fa5731c to 9ed3493 Compare March 6, 2026 15:39
Copy link
Copy Markdown
Collaborator

@alandefreitas alandefreitas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice tests. Everything about this PR is really cool 😎😄

Comment thread src/lib/AST/ExtractDocComment.cpp Outdated
Comment thread include/mrdocs/Metadata/DocComment.hpp
Comment thread src/lib/AST/ExtractDocComment.cpp
Comment thread src/lib/Metadata/Finalizers/FunctionObjectFinalizer.cpp Outdated
Comment thread test-files/golden-tests/javadoc/function-object/function-object.cpp
Comment thread test-files/golden-tests/javadoc/function-object/function-object.cpp Outdated
Comment thread test-files/golden-tests/symbols/variable/function-objects.cpp
@gennaroprota gennaroprota force-pushed the feat/function_object_support branch 3 times, most recently from 554a41f to 28681e1 Compare March 12, 2026 17:40
@gennaroprota gennaroprota marked this pull request as draft March 12, 2026 17:52
@gennaroprota gennaroprota force-pushed the feat/function_object_support branch 7 times, most recently from c72c2b2 to d94c442 Compare March 13, 2026 15:59
When MrDocs encounters a variable that is a "function object" (its type
is a record whose only public non-special members are operator()
overloads), it now synthesizes free-function entries named after the
variable. The type is marked implementation-defined and hidden from the
output. Multi-overload function objects are naturally grouped by
OverloadsFinalizer.

For example:

struct abs_fn {
    double operator()(double x) const noexcept;
};
constexpr abs_fn abs = {};

is documented as if abs were a free function:

double abs(double x) noexcept.

Detection:

Auto-detection: records with only operator() overloads and special
member functions (constructors, destructor, assignment operators).
Controlled by the auto-function-objects config option (default: true).

Explicit: @functionobject or @functor doc commands on the variable or on
the type, e.g. for types that have extra public members. Template types:
auto-detected only when the type lives in the same scope or an inner
scope (e.g. detail::) relative to the variable. This avoids false
positives from unrelated types like std::hash<T>.

What's generated:

Each operator() overload becomes a synthetic FunctionSymbol with the
variable's name, parented under the variable's parent scope. For
variable templates, the synthetic function inherits the variable's
template parameters. A "NOTE: This function object does not participate
in ADL..." disclaimer is appended to each synthetic function's doc.
FunctionSymbol::FunctionObjectImpl back-references the implementation
type.

An example of this feature is added to the landing page (the abs example
above).

Closes cppalliance#564.
This moves isSpecialMemberFunction() out of FunctionObjectFinalizer's
unnamed namespace into Function.hpp/cpp as a shared API, and introduces
individual helpers (isDefaultConstructor(), isCopyConstructor(),
isMoveConstructor(), isCopyAssignment(),
isMoveAssignment())---isSpecialMemberFunction() becomes a composition of
these individual helpers.

Contextually, this fixes three bugs in the existing detection logic in
DocComment/Function.hpp:

- Default constructors with all-default parameters were not recognized
  (only empty parameter lists were handled).

- Default constructors with parameter packs were not recognized.

- By-value copy assignment operator=(X) was not recognized.
@gennaroprota gennaroprota force-pushed the feat/function_object_support branch from d94c442 to a347aa4 Compare March 13, 2026 17:03
@gennaroprota gennaroprota marked this pull request as ready for review March 13, 2026 17:25
@gennaroprota gennaroprota merged commit 0a69301 into cppalliance:develop Mar 16, 2026
30 checks passed
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.

identify niebloids

3 participants