Skip to content

Add support for non-instantiated template rules#14

Merged
nunoplopes merged 4 commits intoCpp2Rust:masterfrom
joaotgouveia:src-parsing-improvements
Apr 21, 2026
Merged

Add support for non-instantiated template rules#14
nunoplopes merged 4 commits intoCpp2Rust:masterfrom
joaotgouveia:src-parsing-improvements

Conversation

@joaotgouveia
Copy link
Copy Markdown
Contributor

Adds support for writing translation rules without having to manually create placeholder classes to instantiate them.

For a rule as follows:

template <typename T1> void f1(T1 first, T1 last) {
  return std::sort(first, last);
}

We must determine what the name std::sort refers 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::sort expects its arguments to be iterator-like, and it cannot be specialized with an empty struct struct 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.

@joaotgouveia joaotgouveia force-pushed the src-parsing-improvements branch 3 times, most recently from abc9d19 to 8a0e2d7 Compare April 20, 2026 17:50
@joaotgouveia joaotgouveia force-pushed the src-parsing-improvements branch from 8a0e2d7 to 471fc20 Compare April 21, 2026 09:11
@nunoplopes nunoplopes merged commit 7b5d4c5 into Cpp2Rust:master Apr 21, 2026
12 of 13 checks passed
@joaotgouveia joaotgouveia deleted the src-parsing-improvements branch April 21, 2026 15:39
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.

2 participants