www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ calling convention only

reply Benjamin Thaut <code benjamin-thaut.de> writes:
Is it possible to declare a function in D which gets the C++ calling 
convetion but not the C++ mangling?

Kind Regards
Benjamin Thaut
Feb 19 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 19 February 2015 at 18:25:10 UTC, Benjamin Thaut 
wrote:
 Is it possible to declare a function in D which gets the C++ 
 calling convetion but not the C++ mangling?

 Kind Regards
 Benjamin Thaut
You can use pragma(mangle, ...) to set whatever mangling you like.
Feb 19 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 19 February 2015 at 21:33:50 UTC, John Colvin wrote:
 On Thursday, 19 February 2015 at 18:25:10 UTC, Benjamin Thaut 
 wrote:
 Is it possible to declare a function in D which gets the C++ 
 calling convetion but not the C++ mangling?

 Kind Regards
 Benjamin Thaut
You can use pragma(mangle, ...) to set whatever mangling you like.
I would duplicate the declaration, once without extern(C++), once with, the use the .mangleof from the 1st to set the mangle of the 2nd with pragma(mangle
Feb 19 2015
parent reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
On Thursday, 19 February 2015 at 21:34:57 UTC, John Colvin wrote:
 I would duplicate the declaration, once without extern(C++), 
 once with, the use the .mangleof from the 1st to set the mangle 
 of the 2nd with pragma(mangle
Yes that would work. But using pragma(mangle) feels so hacky...
Feb 20 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 20 February 2015 at 12:23:31 UTC, Benjamin Thaut wrote:
 On Thursday, 19 February 2015 at 21:34:57 UTC, John Colvin 
 wrote:
 I would duplicate the declaration, once without extern(C++), 
 once with, the use the .mangleof from the 1st to set the 
 mangle of the 2nd with pragma(mangle
Yes that would work. But using pragma(mangle) feels so hacky...
I agree. Wrap it in a mixin / mixin template? Why do you need this? Presumably it'll be hidden in the depths of some library / bindings where beauty is somewhat optional? Using the .mangleof from an extern(D) function should mean it's robust.
Feb 20 2015
parent reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
On Friday, 20 February 2015 at 13:00:39 UTC, John Colvin wrote:
 I agree. Wrap it in a mixin / mixin template?

 Why do you need this? Presumably it'll be hidden in the depths 
 of some library / bindings where beauty is somewhat optional? 
 Using the .mangleof from an extern(D) function should mean it's 
 robust.
Well the use case is creating a function which sole purpose it is to create a function pointer from it and pass it to C++. If it recieves C++ mangling however I have to pay attention that it does not conflict with any other C++ symbols. The same goes for extern(C). Sometimes you want to create functions with a C calling convetion so you can create a function pointer from it. With extern(C) its even a bigger problem because the C mangling conflicts a lot easier.
Feb 20 2015
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 20 February 2015 at 13:40:12 UTC, Benjamin Thaut wrote:
 On Friday, 20 February 2015 at 13:00:39 UTC, John Colvin wrote:
 I agree. Wrap it in a mixin / mixin template?

 Why do you need this? Presumably it'll be hidden in the depths 
 of some library / bindings where beauty is somewhat optional? 
 Using the .mangleof from an extern(D) function should mean 
 it's robust.
Well the use case is creating a function which sole purpose it is to create a function pointer from it and pass it to C++. If it recieves C++ mangling however I have to pay attention that it does not conflict with any other C++ symbols. The same goes for extern(C). Sometimes you want to create functions with a C calling convetion so you can create a function pointer from it. With extern(C) its even a bigger problem because the C mangling conflicts a lot easier.
For C++, you can just use the newly added namespace support: extern(C++, nobody.uses.this.name) myFunc() {}
Feb 21 2015
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 21.02.2015 um 11:30 schrieb "Marc =?UTF-8?B?U2Now7x0eiI=?= 
<schuetzm gmx.net>":
 For C++, you can just use the newly added namespace support:

      extern(C++, nobody.uses.this.name) myFunc() {}
Thats actually a good idea. Thanks.
Feb 21 2015