Title: Correctness bugs: set::min()/max() ignore comparator; non-const set::operator[] does not compile · Issue #28 · jkalias/functional_cpp · GitHub
Open Graph Title: Correctness bugs: set::min()/max() ignore comparator; non-const set::operator[] does not compile · Issue #28 · jkalias/functional_cpp
X Title: Correctness bugs: set::min()/max() ignore comparator; non-const set::operator[] does not compile · Issue #28 · jkalias/functional_cpp
Description: Summary Two correctness defects in include/set.h. Both are currently masked by the test suite (one via a compiler-conditional expectation, one because the buggy overload is never instantiated) rather than fixed. 1. set::min() / max() ign...
Open Graph Description: Summary Two correctness defects in include/set.h. Both are currently masked by the test suite (one via a compiler-conditional expectation, one because the buggy overload is never instantiated) rath...
X Description: Summary Two correctness defects in include/set.h. Both are currently masked by the test suite (one via a compiler-conditional expectation, one because the buggy overload is never instantiated) rath...
Opengraph URL: https://github.com/jkalias/functional_cpp/issues/28
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Correctness bugs: set::min()/max() ignore comparator; non-const set::operator[] does not compile","articleBody":"## Summary\n\nTwo correctness defects in `include/set.h`. Both are currently masked by the test suite (one via a compiler-conditional expectation, one because the buggy overload is never instantiated) rather than fixed.\n\n---\n\n### 1. `set::min()` / `max()` ignore the set's comparator (and are O(n))\n\n**Location:** `include/set.h:607-635`\n\n```cpp\n[[nodiscard]] fcpp::optional_t\u003cTKey> min() const\n{\n const auto& it = std::min_element(begin(), end());\n if (it != end()) {\n return *it;\n }\n return fcpp::optional_t\u003cTKey>();\n}\n```\n\n`std::min_element` / `std::max_element` compare elements with `operator\u003c` on `TKey`, **not** with the set's `TCompare`.\n\n**Consequences**\n- **Wrong result** for any set instantiated with a custom comparator. The set is stored ordered by `TCompare`, but `min()`/`max()` re-rank by `operator\u003c`, so they can return the wrong element.\n- **Fails to compile** for key types that provide `TCompare` but not `operator\u003c`.\n- **O(n)** where it should be **O(1)**: a `std::set` is already ordered, so the minimum is `*begin()` and the maximum is `*std::prev(end())`.\n\n**Evidence — the test papers over this instead of fixing it** (`tests/set_test.cc:289`):\n\n```cpp\nTEST(SetTest, MinCustomType)\n{\n const set\u003cperson, person_comparator> persons({\n person(15, \"Jake\"), person(18, \"Jannet\"),\n person(25, \"Kate\"), person(62, \"Bob\")\n });\n const auto minimum = persons.min();\n#if defined(__clang__)\n EXPECT_EQ(person(18, \"Jannet\"), minimum.value());\n#else\n EXPECT_EQ(person(62, \"Bob\"), minimum.value()); // different answer per compiler\n#endif\n}\n```\n\nThe fact that the expected value differs between clang and gcc is a direct symptom of the bug: the answer depends on `operator\u003c` and iteration order rather than the set's actual ordering.\n\n**Suggested fix**\n\n```cpp\n[[nodiscard]] fcpp::optional_t\u003cTKey> min() const\n{\n if (m_set.empty()) {\n return fcpp::optional_t\u003cTKey>();\n }\n return *begin(); // ordered by TCompare, O(1)\n}\n\n[[nodiscard]] fcpp::optional_t\u003cTKey> max() const\n{\n if (m_set.empty()) {\n return fcpp::optional_t\u003cTKey>();\n }\n return *std::prev(end()); // O(1)\n}\n```\n\nThis is deterministic, respects the custom comparator, is O(1), and allows the `#if defined(__clang__)` branch in `SetTest.MinCustomType` (and the analogous max test) to be removed.\n\n---\n\n### 2. Non-const `set::operator[]` does not compile\n\n**Location:** `include/set.h:1102-1116`\n\n```cpp\nTKey operator[](size_t index)\n{\n assert_smaller_size(index);\n#ifdef CPP17_AVAILABLE\n auto it = std::advance(begin(), index); // std::advance returns void\n return *it;\n#else\n ...\n#endif\n}\n```\n\n`std::advance` returns `void`, so `auto it = std::advance(...)` is ill-formed. The **const** overload (`set.h:1121-1136`) does it correctly:\n\n```cpp\nauto it = begin();\nstd::advance(it, index);\nreturn *it;\n```\n\n**Why it's currently hidden:** every call site in the tests uses `operator[]` on a `const set&` (e.g. `test_contents(const set\u003cint>& set)` at `tests/set_test.cc:37`), so the non-const overload is never instantiated. Any use such as:\n\n```cpp\nfcpp::set\u003cint> s({1, 2, 3});\nauto x = s[0]; // selects the non-const overload -> compile error\n```\n\nwould fail to build under C++17.\n\n**Suggested fix:** mirror the const overload:\n\n```cpp\nTKey operator[](size_t index)\n{\n assert_smaller_size(index);\n auto it = begin();\n std::advance(it, index); // works for both C++11 and C++17 paths\n return *it;\n}\n```\n\nA regression test that subscripts a **non-const** set would prevent this from regressing.\n","author":{"url":"https://github.com/jkalias","@type":"Person","name":"jkalias"},"datePublished":"2026-06-11T15:11:46Z","url":"https://github.com/jkalias/functional_cpp/issues/28"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ec7eb6c1-f250-43c9-b97b-beadaf8437db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94C0:3ABDFB:49E097:655478:6B04704A |
| html-safe-nonce | 59ff48090f466a1b35a62ed0113150930f4f1dc9b8252ff4a7c77387564909f1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEMwOjNBQkRGQjo0OUUwOTc6NjU1NDc4OjZCMDQ3MDRBIiwidmlzaXRvcl9pZCI6IjM3ODE2NzgxODcyMDE5ODI1MzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a2cca087492cd51fe712bc35ed730080d9cb2784e5fdb741ae853eeaf43d87c8 |
| hovercard-subject-tag | issue:4641795730 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/jkalias/functional_cpp/28/issue_layout |
| twitter:image | https://opengraph.githubassets.com/860309cf121f5bd192eefd912f963176b187620df3ea227b602069b6213985fd/jkalias/functional_cpp/issues/28 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/860309cf121f5bd192eefd912f963176b187620df3ea227b602069b6213985fd/jkalias/functional_cpp/issues/28 |
| og:image:alt | Summary Two correctness defects in include/set.h. Both are currently masked by the test suite (one via a compiler-conditional expectation, one because the buggy overload is never instantiated) rath... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jkalias |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| turbo-cache-control | no-preview |
| go-import | github.com/jkalias/functional_cpp git https://github.com/jkalias/functional_cpp.git |
| octolytics-dimension-user_id | 942804 |
| octolytics-dimension-user_login | jkalias |
| octolytics-dimension-repository_id | 387602376 |
| octolytics-dimension-repository_nwo | jkalias/functional_cpp |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 387602376 |
| octolytics-dimension-repository_network_root_nwo | jkalias/functional_cpp |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width