Boost logo

Boost Users :

Subject: Re: [Boost-users] [Format] Asserts with non-default structure packingin 1.37 (MSVC)
From: Daniel James (gmane.junk001_at_[hidden])
Date: 2008-12-31 06:12:05


In article <099818F076834EA28762E7BAB3D97A53_at_fuji>, John Maddock wrote:
> It appears to be a bug in MSVC's __alignof builtin operator ...

Hmm.

Looking more closely I think _alignof does what it says on the tin, but
that the problem is that /ZPn (and also #pragma pack(n) and
__declspec(align(n)) ) only affect the alignment of /structures/ and
not of things like int and char.

This code, compiled without any /Zp switch:

#pragma pack( push, 2 )
int int_2 = 0;
struct s2
{
    int member;
};
#pragma pack( pop )
int int_def = 0;
struct sdef
{
    int member;
};

int __declspec( align(2) ) forced;

int main(int argc, char* argv[])
{
    std::cout << "Alignment of int_2 is " << _alignof( int_2 )
        << std::endl;
    std::cout << "Alignment of int_def is " << _alignof( int_def )
        << std::endl;

    std::cout << "Alignment of s2 is " << _alignof( s2 )
        << std::endl;
    std::cout << "Alignment of sdef is " << _alignof( sdef )
        << std::endl;

    std::cout << "Alignment of forced is " << _alignof( forced )
        << std::endl;

    return 0;
}

prints out:

Alignment of int_2 is 4
Alignment of int_def is 4
Alignment of s2 is 2
Alignment of sdef is 4
Alignment of forced is 4

which appears to indicate that _alignof does correctly report the
alignment of 2-byte aligned objects, but that only the alignment of
structures can be specified by the user ... not sure how I might verify
that, though.

Incidentally, the same behaviour is seen in VS 2008.

> if you comment out the line:
>
> # define BOOST_ALIGNMENT_OF(T) __alignof(T)
>
> In line 108 of boost/type_traits/intrinsics.hpp then it all works as
> expected (I hope!).

Thanks, that's looking good so far.

Cheers,
 Daniel.
 


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net