[Issue 19348] New: Struct casts should be better documented.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 2 12:27:34 UTC 2018


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

          Issue ID: 19348
           Summary: Struct casts should be better documented.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dlang.org
          Assignee: nobody at puremagic.com
          Reporter: stanislav.blinov at gmail.com

struct S { int i;     }
struct X { byte[4] b; }
union  U { S s;       }

void main() {
    U u;
    auto s1 = cast(S)u;
    auto s2 = cast(X)s1;
    //auto s3 = S(u);  // cannot implicitly convert...
    //auto s4 = X(s1); // cannot implicitly convert...
}

Documentation on such casts is lacking.
https://dlang.org/spec/expression.html#cast_expressions says:

8. Casting a value v to a struct S, when value is not a struct of the same
type, is equivalent to: S(v)

...which is obviously not the case, as evidenced by uncommenting the `s3/s4`
lines in the snipped above. Perhaps it should state something like:

8. Casting a value v to a struct S, when v is not a struct of the same type,
creates a rvalue of type S, with representation copied from v; no
postblits/copy constructors are called; v.sizeof must be equal to S.sizeof.

9. Casting a value v to a struct S, when v is also of type S, is equivalent to
making a copy of v. Such a cast will generate an error if appropriate
postblit/copy constructor is @disabled for S.

--


More information about the Digitalmars-d-bugs mailing list