COMMUNITY

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

generic
extern
haxe-cpp

#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.


(Dmitry Hryppa) #2

No, you can’t do this.
But you can specify every type of T that you need manually. Something like:

class Main {
    public static function main():Void {
        var vec:IntVector = IntVector.create();
        vec.pushBack(10);
        cpp.Lib.println(vec.at(0)); //10
    }
}

@:include('vector')
@:native('std::vector')
extern class Vector<T> {
    @:native('push_back') public function pushBack(value:T):Void;
    public function at(index:Int):T;
}

@:structAccess
@:native('std::vector<int>')
extern class IntVector extends Vector<Int> {
    @:native('std::vector<int>') public static function create():IntVector;
}