Add support for non-instantiated template rules#14
Merged
nunoplopes merged 4 commits intoCpp2Rust:masterfrom Apr 21, 2026
Merged
Add support for non-instantiated template rules#14nunoplopes merged 4 commits intoCpp2Rust:masterfrom
nunoplopes merged 4 commits intoCpp2Rust:masterfrom
Conversation
abc9d19 to
8a0e2d7
Compare
8a0e2d7 to
471fc20
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for writing translation rules without having to manually create placeholder classes to instantiate them.
For a rule as follows:
We must determine what the name
std::sortrefers to.First, the rule's template parameters are inspected, and a set of template arguments is synthesized. Each synthesized argument is either an empty struct or a constexpr with an appropriate type, depending on the kind of the corresponding template parameter.
These template arguments are then used to instantiate the rule's function signature, which yields the non-dependent type of each call argument. These types are used to synthesize a set of expressions corresponding to call arguments. Since the synthesized template arguments may not respect all of the constraints of a given template, it's not possible to directly instantiate the body of the rule and simply fetch the returned value. For instance,
std::sortexpects its arguments to be iterator-like, and it cannot be specialized with an empty structstruct T1{};.Afterwards, the name is looked up. For each overload, any implicit template arguments are deduced using the previously obtained synthesized call arguments. The deduced template arguments are combined with any explicit template arguments and used to instantiate the overloads' declaration.
Finally, overload resolution is performed, and the best match is selected.
The only rules that are not yet handled are those that receive a lambda expression as a parameter (e.g. the rule
algorithm/f6), as it is not possible to explicitly write the type of such expressions. Since rule matching is performed by printing rules and comparing strings, I believe that a possible solution would be to print lambda expressions as if they were regular function pointers. This would eliminate the need to write duplicate rules for functions receiving function pointers and functions receiving lambda expressions, but it would also preclude us from writing rules that handle lambda expressions and function pointers differently.