C++ Interfacing:'static' array function parameter contradiction

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 28 17:31:32 PDT 2017


On Friday, 28 April 2017 at 19:08:18 UTC, ParticlePeter wrote:
> On Friday, 28 April 2017 at 17:57:34 UTC, Ali Çehreli wrote:
>> On 04/28/2017 08:56 AM, ParticlePeter wrote:
>> > C++ Function:
>> > bool cppFunc( float[3] color );
>> >
>> > D binding:
>> > extern(C++) bool cppFunc( float[3] color );
>> >
>> > Using with:
>> > float[3] my_color;
>> > cppFunc( my_color );
>> >
>> > -> Error: Internal Compiler Error: unable to pass static
>> array to
>>
>> That part is a bug at least in the compiler message. Is it 
>> really an internal ctompiler error? Doesn't look like it: the 
>> compiler is talking to us happily. :)
>>
>> My simple test works for me:
>>
>> // deneme.cpp
>> float cppFunc(float color[3]) {
>>     return color[0] + color[1] + color[2];
>> }
>>
>> $ g++ -c deneme.cpp -o deneme_cpp.o
>>
>> // deneme.d
>> extern(C++) float cppFunc(float * color);
>>
>> void main() {
>>     float[3] my_color = [ 1.5, 2.5, 3.5 ] ;
>>     assert(cppFunc(my_color.ptr) == 7.5);
>> }
>>
>> $ dmd deneme_cpp.o deneme.d -of=deneme
>>
>> Builds and runs fine... on Linux... I don't know whether 
>> that's significant.
>>
>> Ali
>
> Btw, according to [1] your example should not work either, I 
> doubt that there is a difference between C and C++ interfacing, 
> it should be:
>
> extern(C++) float cppFunc( ref float[3] color );
>
> In my case its a linker error as well.
>
> [1] http://dlang.org/spec/interfaceToC.html#passing_d_array

If you are having problems with the linker with Ali's you can do
```
extern(C++) bool cppFunc( float[3] color ); // correct signature, 
but causes compiler error

pragma(mangle, cppFunc.mangleof)
float cppFunc(float * color); // compatible signature but wrong 
mangling overridden with pragma(mangle,...)




More information about the Digitalmars-d-learn mailing list