locked
Union nested whithin a value struct

    Question

  • Hi, please, how to define a UNION nested within a VALUE STRUCT ?

    Is there a C++/CX equivalent to C++/CLI LayoutKind::Explicit and FieldOffset attributes ?

    Sunday, May 13, 2012 3:52 PM

Answers

  •  

    Hello,

    We can use the union in struct, union is native C++ type and value struct is WinRT type.

    You can use #pragma pack for memory alignment

    http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.80).aspx

    Please try this codes

    union NumericType
    {
        int         iValue;
        long        lValue;  
        double      dValue;  
    };
    #pragma pack(2)
    value struct Testunion sealed
    {
    	union NumericType Values;
    	double size;
    };
    #pragma pack()
    // call this in main function
    	Testunion t;
    	union NumericType temp = { 10 };
    	t.Values=temp; 

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 17, 2012 8:40 AM
    Monday, May 14, 2012 11:22 AM
  • As a workaround, it might be an idea to declare the biggest type in your value struct and cast a pointer of it to a NumericType* when needed, or just directly to the type needed.

    There's a whole lot of restrictions like that in WinRT. They can however be sidestepped creatively, if really necessary. For instance, I'm feeding native pointers in and out of WinRT classes using ints and longs (depending on architecture bit width).

    • Marked as answer by Jesse Jiang Thursday, May 17, 2012 8:40 AM
    Monday, May 14, 2012 4:09 PM

All replies

  •  

    Hello,

    We can use the union in struct, union is native C++ type and value struct is WinRT type.

    You can use #pragma pack for memory alignment

    http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.80).aspx

    Please try this codes

    union NumericType
    {
        int         iValue;
        long        lValue;  
        double      dValue;  
    };
    #pragma pack(2)
    value struct Testunion sealed
    {
    	union NumericType Values;
    	double size;
    };
    #pragma pack()
    // call this in main function
    	Testunion t;
    	union NumericType temp = { 10 };
    	t.Values=temp; 

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 17, 2012 8:40 AM
    Monday, May 14, 2012 11:22 AM
  • Thanks,

    I have not checked the offsets, it seems a good solution for private data.. but when the value struct is marked as public to be passed across the ABI boundary the compiler tell me error C3986: 'Values': signature of member contains native type NumericType.

    Monday, May 14, 2012 12:41 PM
  • As a workaround, it might be an idea to declare the biggest type in your value struct and cast a pointer of it to a NumericType* when needed, or just directly to the type needed.

    There's a whole lot of restrictions like that in WinRT. They can however be sidestepped creatively, if really necessary. For instance, I'm feeding native pointers in and out of WinRT classes using ints and longs (depending on architecture bit width).

    • Marked as answer by Jesse Jiang Thursday, May 17, 2012 8:40 AM
    Monday, May 14, 2012 4:09 PM
  • Thanks for the answers.
    Tuesday, May 15, 2012 12:51 PM