March 28, 2017
On Tuesday, 28 March 2017 at 02:14:25 UTC, Jonathan M Davis wrote:
> Realistically, unless D fully supports C++ (which pretty much means that it has to become C++ on some level), you're almost always going to be stuck with some sort of glue layer between D code and C++ code. There's no reasonable way around that. We can work to improve the situation so that more C++ stuff just works when hooking up to D, but we'll never get all the way there, because that would mean dragging C++ into D, which we really don't want.

I know that. I'm just arguing for this tiny change in C++-mangling of D const object references as a remedy for this one particular issue out of many wrt. C++ interop.

What I don't get is why it's considered important to have a matching C++ mangling for templates across D and C++ - what for? I only care about mangling wrt. interop if I wanna link to some foreign code, and with templates I can't, so I see absolutely no problem with a D template `extern (C++) void foo()(const(Object) arg)` being mangled slightly differently (arg as `const Object*`) than a C++ template `template <typename T> void foo(const T arg)` with `T = const Object*` (arg as `const Object* const`).

Don't get me wrong, I don't advocate C++-mangling a D `const(S*)` for some struct S as `const S*` at all - we're talking specifically about D class references here, which don't have a direct C++ analogon, so choosing to C++-mangle them in a special way sounds absolutely feasible to me.


March 28, 2017
On Tuesday, 28 March 2017 at 08:30:43 UTC, kinke wrote:
> What I don't get is why it's considered important to have a matching C++ mangling for templates across D and C++ - what for? I only care about mangling wrt.

If you still think this is a mangling problem, please reread my first response in this thread.
March 28, 2017
On Tuesday, 28 March 2017 at 12:55:02 UTC, deadalnix wrote:
> On Tuesday, 28 March 2017 at 08:30:43 UTC, kinke wrote:
>> What I don't get is why it's considered important to have a matching C++ mangling for templates across D and C++ - what for? I only care about mangling wrt.
>
> If you still think this is a mangling problem, please reread my first response in this thread.

You don't seem to get my point, I don't know why it's apparently that hard. I don't want to be able to express both `const T* const` AND `const T*` in C++, I only want D's const(Object) mangling to express solely the former instead of the latter, as there's no use for the double-const, except maybe for templates as Walter pointed out, but there's no template interop between D and C++ anyway.

I absolutely don't care that it's inconsistent to what a D const(Object) reference actually is (both pointer and class const) when passing such a thing BY VALUE to C++. As I said, there's no C++ analogon for D object references, so why not have it be a special case in the C++ mangler...
March 28, 2017
On Tuesday, 28 March 2017 at 13:18:57 UTC, kinke wrote:
> You don't seem to get my point, I don't know why it's apparently that hard.

It's hard because you assume I did not understood you point and you keep repeating the same thing. I understand you point and showed you why it isn't a mangling problem at all, and gave you direction you may want to dig in to make a proposal that may actually get traction.

But you can chose to entrench yourself in a position were nobody understands your genius. You won't get any result, but depending on your personality, it may make you feel good, which is already something.

March 28, 2017
On Tuesday, 28 March 2017 at 14:11:16 UTC, deadalnix wrote:
> I understand you point and showed you why it isn't a mangling problem at all, and gave you direction you may want to dig in to make a proposal that may
actually get traction.

If you really did get my point, it should be clear that I don't see a necessity for differentiating between `const T` and `const(T)` on the D side in general, transitive const hasn't been an issue for my D/C++ interop use cases yet, but the const(Object) issue the OP arises would definitely be a show stopper at some point and, unlike a more general solution, can be trivially remedied.
March 28, 2017
On 2017-03-28 00:30, kinke wrote:

> Yep, so there are no libs my D code can link to, so how am I supposed to
> use C++ templates from D (as you're using that as argument for the
> `const T *const` mangling)?

As far as I understand, D can link with C++ templates if they're instantiated on the C++ side. It would link with the instantiated template not the actual template.

-- 
/Jacob Carlborg
March 28, 2017
On 3/28/2017 11:12 AM, Jacob Carlborg wrote:
> As far as I understand, D can link with C++ templates if they're instantiated on
> the C++ side. It would link with the instantiated template not the actual template.

Recall that dmd itself connected to C++ templates when the front end was not full in D.

August 26, 2020
On Monday, 27 March 2017 at 20:41:51 UTC, kinke wrote:
> On Monday, 27 March 2017 at 20:09:35 UTC, Walter Bright wrote:
>> Whichever way it is mangled will gore someone's ox. D went with the simplest mangling solution, which is to mangle all C++ const pointers as "head const".
>> [...]
>> I suggest a simpler way - declare the C++ side of the D interface in a way that matches the way D mangles it. It's always been true that in order to interface D with C++ you'll need to be a bit flexible on the C++ side.
>
> Unfortunately, it's almost always the other way around - D code trying to interop with one of the gazillions existing C++ libs, and nobody wants to maintain his own fork with D-compatible glue interfaces. How often did you use `const T *const` vs. `const T *` in your C++ headers? ;) I think this would be a tiny change for D, breaking almost no code and well worth the reduction in required 'flexibility on the C++ side'.

I just ran into this problem too. Is there some way around this with pragma(mangle)? Forking the 3rd-party library would be my last resort. If there's a way that I can write the declaration in D I would prefer to do that.
August 26, 2020
On Tue, Mar 28, 2017 at 11:21 PM kinke via Digitalmars-d < digitalmars-d@puremagic.com> wrote:

> On Tuesday, 28 March 2017 at 12:55:02 UTC, deadalnix wrote:
> > On Tuesday, 28 March 2017 at 08:30:43 UTC, kinke wrote:
> >> What I don't get is why it's considered important to have a matching C++ mangling for templates across D and C++ - what for? I only care about mangling wrt.
> >
> > If you still think this is a mangling problem, please reread my first response in this thread.
>
> You don't seem to get my point, I don't know why it's apparently that hard. I don't want to be able to express both `const T* const` AND `const T*` in C++, I only want D's const(Object) mangling to express solely the former instead of the latter, as there's no use for the double-const, except maybe for templates as Walter pointed out, but there's no template interop between D and C++ anyway.
>
> I absolutely don't care that it's inconsistent to what a D const(Object) reference actually is (both pointer and class const) when passing such a thing BY VALUE to C++. As I said, there's no C++ analogon for D object references, so why not have it be a special case in the C++ mangler...
>

I've lost sleep on this point for many years. I keep changing my mind
between purity and what's actually useful, but I've settled on what you say
here.
It is my opinion from a decade of experience that D should mangle
const(Class) as `const Class *`. It is what you want 99% of the time.


There's some precedent for this too; C++ actually strips the head const off
of the types when mangling function arguments.
For instance:
  void t(const int *);
  void t(const int * const);
Both functions mangle to `void t(const int *)` (and this is an invalid
overload). The pointer const is not present in the mangling here.
We follow this rule when mangling const class arguments to functions. In
this case, we strip the pointer const from the mangling (as does C++).

The case where it can go either way is with templates:
  void foo<const Class * const>();
  void foo<const Class *>();
These are valid overloads... and D can only express the former
instantiation, which basically never occurs in C++.

I accept that they are functionally different things, and 'correct-ness'
says that we should indeed mangle the former one, but that's just not
useful.
I think even in the template case, we should mangle const(Class) to `const
Class *`, and not `const Class * const`.
It is possible to conjure an exploit where C++ may modify a const D
variable across the binding, but that's extremely contrived, and the
capabilities our current mangling limits us from is an endless pain in the
arse.


August 26, 2020
On 3/26/2017 3:43 AM, Benjamin Thaut wrote:
> Should I open a bug for this?

https://issues.dlang.org/show_bug.cgi?id=21200