How to correctly fix “zero-sized array in struct/union” warning (C4200) without breaking the code?

后端 未结 6 1456
北海茫月
北海茫月 2021-02-05 07:52

I\'m integrating some code into my library. It is a complex data structure well optimized for speed, so i\'m trying not to modify it too much. The integration process goes well

6条回答
  •  不要未来只要你来
    2021-02-05 08:07

    Although I realise that this is an old thread, I would like to give my pure c++11 solution to the OP's question. The idea is to wrap the to be allocated object, adding in the padding to align the objects in array to power of 2 adresses, in the following way:

    template
    struct PaddedType : private T { private: char padding [ ObjectPaddingSize ]; };
    
    template // No padding.
    struct PaddedType : private T { };
    
    template
    struct PaddedT : private PaddedType::value - sizeof ( T )> { };
    

    The objects padding size can be calculated at compile-time with the following class (returns L if L is power of 2, else the next power of 2 gt L):

    template
    class NextPowerOfTwo {
    
        template 
        struct NextPowerOfTwo1 {
    
            enum { value = NextPowerOfTwo1::value };
        };
    
        template 
        struct NextPowerOfTwo1 {
    
            enum { value = M << 1 };
        };
    
        // Determine whether S is a power of 2, if not dispatch.
    
        template 
        struct NextPowerOfTwo2 {
    
            enum { value = NextPowerOfTwo1::value };
        };
    
        template 
        struct NextPowerOfTwo2 {
    
            enum { value = M };
        };
    
    public:
    
        enum { value = NextPowerOfTwo2::value };
    };
    

提交回复
热议问题