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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

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