3

Let's say in C++ i have a function with the following signature:

A<B> getTemplatedClassInstance();

where A & B are some classes. I am then trying to generate WebIDl bindings for use in an emscripten build to use the C++ code from Javascript. I have a working bindings file with various other classes, enums, etc that can be bound and accessed from JS, but I'm at a loss for how to represent templated types (class A, in the above example). I have tried various combinations of WebIDL binding declarations like:

interface A {};
interface A<> {};
interface A<B> {};

interface C {
A<B> getTemplatedClassInstance();
};

interface C {
A getTemplatedClassInstance();
};

But any use of angle brackets '<>' generates a syntax error from the webidl_binder.py script (included with the emscripten sdk) used to build it, and if I leave the declaration and uses of A as untemplated, the bindings file builds, but the final emscripten compilation (emcc ...) of the bindings with the C++ library fails in the bindings cpp file with:

error: use of class template requires template arguments

which makes sense. Oddly the WebIDL documentation doesn't mention template types at all. I scoured the web and found a single reference to binding specific template instantiations with "Embind", but nothing regarding WebIDL.

dphil
  • 31
  • 3
  • I'm dealing with the same issue here. I'm thinking of create a custom class that extends the templated class but with a concrete type ie class FooBar: Foo and use that in the webidl definition. I haven't tested it though. – Zubzub Dec 26 '17 at 20:51
  • 3
    @Zubzub I had forgotten to come back and resolve this question, but I found a solution. You can just typedef the template types. For example, I created a separate template_typedefs.h file that has lines like: `typedef A class_A_of_B`, and then `include` that file in the build and you can freely use 'class_A_of_B' as the concrete type in the idl file. Hope that helps. – dphil Dec 27 '17 at 22:12
  • If you'd like to use `std::vector`, do you have to redefine it in the `.idl` for each type? – JMRC Jun 21 '23 at 04:21

0 Answers0