Passing D reimplementation of C++ template as argument to a C++ function

Gregor Mückl gregormueckl at gmx.de
Sat Sep 24 07:04:34 UTC 2022


Hi!

I have a D template struct that reimplements a C++ class template 
with identical memory layout for a set of types that matter to 
me. Now, I want to use some C++ functions and classes that use 
these template instances, from D. For that, I want to 
purposefully alias the D and C++ types. However, with the C++ 
type being templated, I don't know how to name that type in a 
extern declaration in D.

Consider, the following simplified example:

```cpp
// C++ template class (from third party library)
template<typename T, typename X>
class Foo {
   // X is made part of the type as a behavior switch, but is 
never instanced
   T a, b;
};

using FooFloat = Foo<float>;

// own C++ function:
void bar(FooFloat f);

```

```d
// D template struct
struct Foo(T) {
   T a, b;
}

alias FooFloat = Foo!float;

// How do I correctly bind the C++ function bar here?

```

Ideally, I want to pass an instance of the D implementation of 
FooFloat without introducing additional wrapper functions.

I know that the whole thing does sound quite mad at first glance 
and screams "bad idea!", but there is a reason why I want to try 
to tread into this minefield.


More information about the Digitalmars-d-learn mailing list