How do you create a static function with a generic parameter in hxcpp externs? (Template Meta-Programming)


#1

How do you create a static function in a hxcpp extern where the original C++ code has a template generic parameter for a class object that you pass into the function?

For example how to turn something like this (C++):

template<typename T, T Value, class Cls>
void funcName(){}

called like this (C++):
funcName<ClassName>();

to something like this (haxe HXCPP extern):

@:generic
@:native('functionName')
static function funcName<T>(className:T) : Void;

If this isn’t implemented yet in HXCPP externs, then it is a major oversight because it appears to be a common C++11 feature used in many modern C++ libraries.


#2

How is the final code generated? Perhaps it generates different versions based upon the type field? This is how Haxe generics work, like Vector<Int> becomes _Vector__Int, etc. Maybe C++11 does something similar under the hood?


#3

If you mean my example extern function, I’m not sure if it’s anywhere close to the correct way to do it. If I have the @:generic metadata it just gives a recursive generic compilation error in Haxe, and when I remove it the end result doesn’t look like what I’d hope in C++, where it’s effectively just an argument and nothing changes.


HXDLIN(  74) testFunction(hx::ClassOf< ::testnamespace::ClassName >(),tmp,HX_("ClassName",66,96,2d,b3));

The hacky solution for now has been to just used untyped __cpp__("functionName<ClassName>()") but that comes with a whole host of issues such as weak typing, learning the generated haxe namespaces, passing the namespace AND the class name AND the method name into the untyped __cpp__() extern function to be string interpolated in the untyped called, and all sorts of inconveniences and potential for bugs.