Why can't we use strings in C++ methods?

Dadoum dadoum at protonmail.com
Sat Nov 4 14:32:04 UTC 2023


On Saturday, 4 November 2023 at 14:21:49 UTC, Paul Backus wrote:
> [...]
>
> `extern(C++)` functions use C++ name mangling, which includes 
> the types of the parameters in the mangled name. However, since 
> C++ does not have a built-in slice type like D's `T[]`, there 
> is no valid C++ mangling for a D slice. Because of this, it is 
> impossible to compile an `extern(C++)` function that has a D 
> slice as a parameter.
>
> As a workaround, you can convert the slice to a `struct`:
>
> [...]

I use another workaround myself:

```d
extern (C++) struct List(T) { // or extern (C)
         T[] self;
         alias self this;
}

extern (C++) void hello(List!ubyte arg) {
         import std.stdio;
         writeln(arg);
}
```

but it means adding a lot of edge cases to the mixins I use for 
bindings while showing that C++ can express D arrays.

(translated to

```c++
template <typename T>
struct List final
{
     _d_dynamicArray< T > self;
     List()
     {
     }
};

extern void hello(List<uint8_t > arg);
```

)


More information about the Digitalmars-d-learn mailing list