Thread overview
[Issue 19179] extern(C++) small-struct by-val uses wrong ABI
Aug 19, 2018
Manu
Sep 07, 2018
Mike Franklin
Sep 10, 2018
Jack
Mar 04, 2019
Manu
Aug 29, 2020
Walter Bright
Sep 06, 2020
Dlang Bot
Sep 08, 2020
Dlang Bot
Sep 20, 2020
jacob
Nov 10, 2020
Walter Bright
August 19, 2018
https://issues.dlang.org/show_bug.cgi?id=19179

Manu <turkeyman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |C++, industry, wrong-code

--- Comment #1 from Manu <turkeyman@gmail.com> ---
Oops, my cut&paste removed the `extern(C++):` at the top of the D source.
Those 2 test functions should be extern(C++)!

--
September 07, 2018
https://issues.dlang.org/show_bug.cgi?id=19179

Mike Franklin <slavo5150@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |slavo5150@yahoo.com
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=5570

--
September 10, 2018
https://issues.dlang.org/show_bug.cgi?id=19179

Jack <look.at.me.pee.please@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |look.at.me.pee.please@gmail
                   |                            |.com

--- Comment #2 from Jack <look.at.me.pee.please@gmail.com> ---

Can't reproduce with your example, I've come across something similar in the past though. If you swap your struct with this one:


struct SmallStruct {
        int x, y;

        SmallStruct()
        {
                x = 10;
                y = 20;
        }
};

static_assert(!std::is_pod<SmallStruct>::value, ""); // not a POD, your other
struct is a POD though


the D version will be POD though. But since you can't have a default constructor in D, you have to use another method to make it not POD. That is give it a destructor "~this()" or a copy constructor "this(this)".

Windows treats structs that aren't POD differently if they are less than 8 bytes. That's the root of the problem.

A solution would be to add an attribute "@NotPOD struct blah { }" or something, rather than having to put an empty constructor. Not exactly something that is easy to notify the user of if this inconsistency exists.

--
March 04, 2019
https://issues.dlang.org/show_bug.cgi?id=19179

--- Comment #3 from Manu <turkeyman@gmail.com> ---
I think what should probably happen here, is that while the type in D is POD, the counterpart in C++ is not POD because default initialisation generates a constructor.

Perhaps the proper solution is to make the D struct emit a default constructor (this just assign's init), and also force the type to use the non-POD ABI? This way semantics will match C++, and if the C++ class externs to the default constructor; it will link as they expect.

--
August 29, 2020
https://issues.dlang.org/show_bug.cgi?id=19179

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |backend
                 CC|                            |bugzilla@digitalmars.com

--
September 06, 2020
https://issues.dlang.org/show_bug.cgi?id=19179

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright created dlang/dmd pull request #11698 "fix Issue 19179 - extern(C++) small-struct by-val uses wrong ABI" fixing this issue:

- fix Issue 19179 - extern(C++) small-struct by-val uses wrong ABI

https://github.com/dlang/dmd/pull/11698

--
September 08, 2020
https://issues.dlang.org/show_bug.cgi?id=19179

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #11698 "fix Issue 19179 - extern(C++) small-struct by-val uses wrong ABI" was merged into master:

- b6f570491c6972aa865d1f2e6e19535127a58a06 by Walter Bright:
  fix Issue 19179 - extern(C++) small-struct by-val uses wrong ABI

https://github.com/dlang/dmd/pull/11698

--
September 20, 2020
https://issues.dlang.org/show_bug.cgi?id=19179

jacob <look.at.me.pee.please@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |---

--- Comment #6 from jacob <look.at.me.pee.please@gmail.com> ---
Please read the comments. If you had, you'd have known that the test Manu gave didn't contain the bug.

--
November 10, 2020
https://issues.dlang.org/show_bug.cgi?id=19179

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |INVALID

--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> ---
Default initialization in C++ doesn't make it non-POD. And I added the `extern(C++)` to the test cases that Manu had omitted.

Is the actual problem trying to create a non-POD struct in D that is compatible with a non-POD struct in C++?

As to how to make a POD in D without a default constructor, just add a trivial postblit:

    this(this) { }

and it will be not POD.

If there's still a problem, please file a new report explaining what that is rather than reopening this as a different issue.

--