Search code examples
c++visual-studiodeclspec

Is there a way to use __declspec(align) for for declaring of objects with different type


Is there a way to use __declspec(align) for declaring lot of objects in Visual studio? Sth like:

__declspec(align)
{
int p1;
long p2
}

My question is similar to __declspec(align) for multiple declarations. But this question is for objects with same type.


Solution

  • No, you can't apply a single __declspec(align) to a block of unrelated declarations like this. Each individual declarator statement needs its own __declspec:

    Syntax

    __declspec( align( # ) ) declarator  
    

    You are probably looking for #pragma pack instead:

    Syntax

    #pragma pack( [ show ] | [ push | pop ] [, identifier ] , n  )  
    

    For example:

    #pragma pack(push, 4)
    int p1;
    long p2
    #pragma pack(pop)
    

    #pragma pack(push, 16)
    float rF[4];
    float gF[4];
    float bF[4];
    #pragma pack(pop)
    
    /*
    Alternatively:
    
    #pragma pack(push, 16)
    typedef float floatArray4[4];
    #pragma pack(pop)
    
    floatArray4 rF;
    floatArray4 gF;
    floatArray4 bF;
    */