Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support anonymous template parameters #742

Merged
merged 3 commits into from
Feb 11, 2024

Conversation

HGuillemet
Copy link
Contributor

@HGuillemet HGuillemet commented Feb 8, 2024

Add support for anonymous type template parameters, such as:

template <typename ModuleType, typename>

We use a dummy name [2] as a key in the template map. If we used null or the empty string, we couldn't have more than 1 anonymous parameter.

Regression tests: no change in existing presets.

@saudet
Copy link
Member

saudet commented Feb 9, 2024

Why square brackets? If we want something that looks like it's for templates, why not <...>?

@HGuillemet
Copy link
Contributor Author

We must use something that cannot be an identifier, and that encodes the position of the parameter in the list.
Now it could be anything but I would also avoid something that could be the value of a template parameter, even if I don't think it'll break something, just to avoid confusion. <2> could be a value in case of nested templates.

@HGuillemet
Copy link
Contributor Author

It could be useful to also amend TemplateMap.toString to generate a string that is equivalent to what was parsed (generate typename instead of [2], @2, $2$ or whatever "private" encoding we use).
TemplateMap.toString is used to build fullnames of templated functions.

@saudet
Copy link
Member

saudet commented Feb 9, 2024

Yeah, no, let's not try to encode anything. Let's just do as with functions, that is arg0, arg1, arg2, ... If that causes a conflict at some point, let's get back to it at that point.

@HGuillemet
Copy link
Contributor Author

Why do you want to risk a conflict when we can avoid it ?
Parsing of template<typename arg1, typename> would fail in your case.
What's the problem with using somethong like @arg1 ?

@saudet
Copy link
Member

saudet commented Feb 9, 2024

Meh, ok, whatever, @arg0, etc sounds OK. It just won't allow us to use them as identifiers if we need them for some reason

@HGuillemet
Copy link
Contributor Author

In what situation could we use this placeholder as an identifier ?

@HGuillemet
Copy link
Contributor Author

Can we finalize this ?
I think we just need something to act as a key for the template map. Something depending on the position of the parameter, and something that won't clash with explicit names given to other parameters.
I don't think we will ever get() from the template map using this key.
But I may overlook some cases.

@saudet
Copy link
Member

saudet commented Feb 10, 2024

We could potentially attach some Info to them at some point, and in that case we're already using the slash for "basic/containers" and "basic/types", so what about "template/arg0", ... Good enough?

@HGuillemet
Copy link
Contributor Author

HGuillemet commented Feb 10, 2024

Why not. Or typename/0.
But I don't think we need to make this crafted illegal name visible from presets author.
If we change TemplateMap.toString with something like:

    public String toString() {
        String s = "<";
        for (Map.Entry<String, Type> e : entrySet()) {
            if (s.length() > 1) {
                s += ",";
            }
            Type t = e.getValue();
            if (t == null) {
                String k = e.getKey();
                s += k.startsWith("@") ? "typename" : k; // HERE
            } else {
                s += t.cppName;
            }
        }
        if (s.endsWith(">")) {
            s += " ";
        }
        return s + ">";
    }

Then if we want to attach an info to this function template:

template <typename ModuleType, typename>
void f(ModuleType m);

before it's instantiated, we use:

new Info("f<ModuleType, typename>(ModuleType)")

(f<typename ModuleType, typename>, or even better f<typename,typename>, would be more logical but this would need that we distinguish type and non-type parameters in templateMap, which we do not. We could, with minor changes, but is it worth ? Any other use for this distinction ?).

@saudet
Copy link
Member

saudet commented Feb 10, 2024

Yeah ok, still doesn't justify putting garbage in the map. It occurs to me that we could just put "typename arg0", etc, with a space in it, right?

@HGuillemet
Copy link
Contributor Author

ok done

@saudet saudet merged commit 9940c31 into bytedeco:master Feb 11, 2024
11 checks passed
@HGuillemet HGuillemet deleted the anonymous_template_params branch February 12, 2024 08:26
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