Why don't anonymous joins contain members with non-trivial constructors / destructors?

I could be wrong, but the main explanation I discovered was that the union cannot initialize because it does not know which member constructor needs to be called. The compiler cannot automatically generate a constructor for concatenation.

Why is the user not allowed to define a union constructor? This will remove the indicated problem and allow the presence of union members that have a non-trivial constructor / destructor.

Also, why doesn't a union member have any custom constructors? The previous explanation does not match custom constructors.

Update 1:

Example:

struct SQuaternion
{
    union
    {
        S3DVector Axis;
        struct
        {
            float X;
            float Y;
            float Z;
        };
    };
    float W;
};

. , . , ? , , . , ...

2: , . , , . , , ...

+5
5

: , . , .

, ++ 0x , . ( , , - -, .)

+13

, ​​, ​​ , :

  • ?
  • , ?
  • -pod?
  • , ?
  • ? (union { char c[4]; int n; } u; u.n=1234; cout << u.c[1];).
  • , ?
  • , - ?

( - ?)

, .

+2

, , ++ . C, , ++.

union ++ . , :

union X {
    int i;
    std::string s;
};

X x;
x.s = "Hello";
x.i = 23;

X , ~string 23. , - , . POD. , .

, ++ C-, POD, , - POD. , .

, , , , , , , , .

char , / .

- , , . - . char , ( : ).

, , , , , int a, float b, string c , - ( ), , . x.i() = 23 x.i = 23.

Boost.Variant.

+1

:

typedef union uAA {
    double dVal;
    int iVal[2];

    uAA() : dVal(3.22) {}
} UAA;

main() {
    UAA rdata;

    printf("Array output: %d %d \nDouble output: %lf \n",
        rdata.iVal[0], rdata.iVal[1], rdata.dVal);
}
0

, - , , . - , , .

Re- : 9.5

- ( ), (10.3) . . . (12.1) (12.8), (12.4) (13.5.3, 12.8) ,

0
source

All Articles