Title: Functional Functions - A Comprehensive Proposal Overviewing Blocks, Nested Functions, and Lambdas for C
Shortname: XXX4
Revision: 0
!Previous Revisions: None
Status: P
Date: 2024-01-02
Group: WG14
!Proposal Category: Change Request, Feature Request
!Target: C2y
Editor: JeanHeyd Meneide, phdofthehouse@gmail.com
Editor: Shepherd (Shepherd's Oasis LLC), shepherd@soasis.org
URL: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Functional%20Functions.html
!Paper Source: GitHub
Issue Tracking: GitHub https://github.com/ThePhD/future_cxx/issues
Metadata Order: Previous Revisions, Editor, This Version, Paper Source, Implementation, Issue Tracking, Project, Audience, Proposal Category, Target
Markup Shorthands: markdown yes
Toggle Diffs: no
Abstract: Nested Functions (GCC), Blocks (Clang & Apple-derived compilers), Wide Function Pointers (Borland and existing C library functions), and Lambdas (C++) provide a series of takes on how to, effectively, bundle functions with data in different ways and transport that information to the caller. This proposal goes through the existing practice and enumerations their tradeoffs so as to propose the best possible solution to the problem space at hand.
path: resources/css/bikeshed-wording.html
# Changelog # {#changelog}
- Initial release. ✨
# Introduction and Motivation # {#intro}
Meow. :3
Bark. :3
# Design # {#design}
## Statement Expressions ## {#design-statement.expressions}
From lak; https://gist.github.com/LAK132/0d264549745e8196df1e632d5b518c37
## Nested Function Rehabilitations ## {#design-nested.functions}
```cpp
int main () {
int x = 3;
int zero () {
// OK, no external variables used
return 0;
}
int double_it () {
return x * 2; // constraint violation
}
int triple_it () _Capture(x) {
return x * 3; // OK, x = 3 when called
}
int quadruple_it () _Capture(&x) {
return x * 4; // OK, x = 5 when called
}
int quintuple_it () _Capture(=) {
return x * 5; // OK, x = 3 when called
}
int sextuple_it () _Capture(&) {
return x * 6; // OK, x = 5 when caled
}
x = 5;
return zero() + triple_it() + quadruple_it()
+ quintuple_it() + sextuple_it();
// return 74;
// 0 + (3 * 3) + (5 * 4)
// (3 * 5) + (5 * 6)
}
```
Upsides:
- Fixes ABI and allows Clang et. al. to implement.
- Provides for explicit captures with a keyword.
- Retains Nested Function look and feel.
- Name mangling is less unclear (nested functions have a name, can be used to do recursion unlike Lambdas).
Downsides:
- Nested Function is an "object" now, with a lifetime and a size? (No other function-like thing in C or C++ behaves like this!)
- It's still Nested Functions, GCC will keep their extension and obviously that lures people into executable stack issues.