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

Feasibility of std::array with Set #587

Closed
gyuro opened this issue Sep 3, 2024 · 3 comments
Closed

Feasibility of std::array with Set #587

gyuro opened this issue Sep 3, 2024 · 3 comments

Comments

@gyuro
Copy link

gyuro commented Sep 3, 2024

I'm trying to call the print_values function in Codon, but the value of a is printed as zero, while b is 3 and c is 4.

In C:

extern "C" void print_values(const std::array<double, 2>& a, double b, double c)
{
    // TODO
}

In Codon:

from C import LIBRARY.print_values(Set[float], float, float)

print_values({1, 2}, 3, 4)

This happened with const double[2] a. Could you help me find a solution?

@inumanag
Copy link
Contributor

Hi @gyuro

Set is completely different structure. To make it compatible C, you would have to cast it to list and then pass it as a pointer, e.g.:

from C import LIBRARY.print_values(ptr[float], i32, float, float)
foo = {1, 2}
foo_l = list(foo)
print_values(foo_l.arr.ptr, len(foo_l), 3, 4)

In C, you would have to use

void print_values(double *a, int a_sz, double b, double c) {
...}

I don't know how is std::array<> represented; in general, all extern "C" functions should only deal with C-compatible types (Codon has no C++ interop).

@gyuro
Copy link
Author

gyuro commented Sep 24, 2024

@inumanag, thanks for the comments.

Based on my observations, declaring a tuple value and passing it by pointer to a C function works fine, as shown below, even though Codon lacks C++ interop:

foo = (1, 2)
print_values(__ptr__(foo), 3, 4)

However, my question is about how to pass an array or any container by value to a C function on the Codon side, like this:

foo = (1, 2)
print_values(foo, 3, 4)

@inumanag
Copy link
Contributor

In a nutshell, you cannot---you would need to mock up Codon's type internal structure in C (can be done but is not supported nor recommended). The best way is to cast everything to lists and then pass list pointers with sizes.

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

No branches or pull requests

2 participants