leanprover / leanprover/fp-lean

Section 3: C++ might not be a good example for limitations of overloading

Open
#278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lean
Stars
192
Forks
73
PR merge metrics
No merged PRs in 30d

Description

In C++, Java, C# and Kotlin, multiple implementations of a method are allowed, with differing numbers and types of arguments. The compiler uses the number of arguments and their types to determine which overload was intended. Function and operator overloading has a key limitation: polymorphic functions can't restrict their type arguments to types for which a given overload exists.

maybe true for Java, C#, and Kotlin, but in C++ you can restrict your type arguments to require a specific overload:

#include <utility>

void overloaded(int) {}
void overloaded(char*) {}

template<class T> void other()
requires requires { overloaded(std::declval<T>()); } {}

int main() {
    other<int>();      // compiles
    other<char*>();    // compiles
    // other<int*>();  // does not
}

and even before constraints you’d probably never do this:

Instead, this second method must itself be overloaded for each type that has an overload of the original method, resulting in many boilerplate definitions instead of a single polymorphic definition.

you’d just use a template function and let it fail somewhere in instantiation.

C++-style ad-hoc polymorphism has other problems, but these particular ones I think are not relevant.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Section 3, where the C++, Java, C# and Kotlin overloading example is discussed. Check the stated limitation against the supplied C++ constraints example and decide whether the C++ example should be revised or removed; done means the section accurately describes which limitation applies to each language.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.