Skip to content

support template instantiation using integral values #137

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

Conversation

Vipul-Cariappa
Copy link
Collaborator

Hi @vgvassilev, the string parsing is required here. Because cppyy allows instantiation using strings. For example:

In [1]: import cppyy

In [2]: from cppyy import gbl

In [3]: cppyy.cppdef("""
   ...: template<typename T, int N>
   ...: T add(T n) { return n + (T)N; }
   ...: """)
Out[3]: True

In [4]: gbl.add[int, 5](5)
Out[4]: 10

In [5]: gbl.add["int", "6"](5)
Out[5]: 11

In [6]: gbl.add["int, 7"](5)
Out[6]: 12

Enables 5 tests:

test_stltypes.py::TestSTLTUPLE::test01_tuple_creation_and_access
test_stltypes.py::TestSTLTUPLE::test03_tuple_iter
test_stltypes.py::TestSTLTUPLE::test04_tuple_lifeline
test_templates.py::TestTEMPLATES::test02_non_type_template_args
test_templates.py::TestTEMPLATES::test03_templated_function

static inline
bool is_integral(std::string& s)
{
if (s == "false") { s = "0"; return true; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need a clang::Lexer here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaking clang::Lexer into clingwrapper? I believe that should be abstracted into CppInterOp...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that is going to be an overkill and yes, @aaronj0 is right -- we can't really do it. The question is if we want to support that case? Especially the comma separated argument list in a form of string...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am really not seeing if In [6]: gbl.add["int, 7"](5) is something useful and worth supporting. In cppyy upstream it was easy to implement but it is really worth implementing it. cc: @wlav.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a legitimate use, but an unlikely one b/c both "int" and "7" are valid Python objects, so no quotes needed. I.e., writing "int, 7" is more work than writing int, 7 and thus unlikely. I would also assume that in practice the integer is programmatically established, in which case again it's be a number and not a string.

I don't have a test case that looks like this, which may mean no-one ever asked for it, but that may also be b/c it simply worked.

The only case I may see a use, is if the literal is typed. So e.g. if the templated function is overloaded with the second argument being either C++ double or C++ float and it is expected that the literal is either 3.14 or 3.14f or some such. You can't do that in python: it'd have to be a string. (A workaround could be to use a numpy.float32.)

All that said, see it as a challenge. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in that case we will pass it as “3.14f” instead of “something, something, 3.14f”. The former is way simpler to support than the latter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move forward with the proposed approach. Can we add a fixme capturing the debate whether we want to support that feature.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note about the double/float comments.
double and float are not accepted as non-type template parameters.
Only integers and integer-like (enums) are allowed.
https://godbolt.org/z/71e4Tzevh

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but then I'd make the same comment about 3 and 3u.

Copy link
Collaborator

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if the bots are happy.

@Vipul-Cariappa
Copy link
Collaborator Author

@vgvassilev, I have pushed a comment linking to the comment you have made on supporting template instantiation using string. If you think that is sufficient, then we can merge this. Will enable 5 tests.

@vgvassilev
Copy link
Collaborator

@vgvassilev, I have pushed a comment linking to the comment you have made on supporting template instantiation using string. If you think that is sufficient, then we can merge this. Will enable 5 tests.

Are we going to ignore the bots?

@Vipul-Cariappa
Copy link
Collaborator Author

Hi @vgvassilev, I will go ahead and merge in PR in few minutes. The failures in cling need the tags to be update to not run the tests. I.e. previous xfails are now crashing in cling.

@Vipul-Cariappa Vipul-Cariappa merged commit 9e1b9d7 into compiler-research:master May 14, 2025
39 of 42 checks passed
@Vipul-Cariappa Vipul-Cariappa deleted the dev/integral-template-types branch May 14, 2025 12:32
Vipul-Cariappa added a commit to Vipul-Cariappa/cppyy-compiler-research that referenced this pull request May 14, 2025
Vipul-Cariappa added a commit to Vipul-Cariappa/cppyy-compiler-research that referenced this pull request May 14, 2025
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.

4 participants