CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Why doesn't 'sizeof' work here?

    Is 'sizeof' evaluated at compile time or run time? If it's at compile time, why doesn't this compile with VC8..?

    Code:
    __declspec(align(sizeof(int))) struct s {
    	char a;
    	int  b, c;
    };
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why doesn't 'sizeof' work here?

    Quote Originally Posted by John E View Post
    Is 'sizeof' evaluated at compile time or run time? If it's at compile time, why doesn't this compile with VC8..?

    Code:
    __declspec(align(sizeof(int))) struct s {
    	char a;
    	int  b, c;
    };
    "Compile time" doesn't automatically mean whatever you can do in C++ at compile time, you can stick the value into something that is a Microsoft extension.

    The sizeof() is not part of the C++ preprocessor -- the phase of compilation where sizeof() is determined is done after preprocessing and more than likely after the Microsoft extensions are established. It is no more different than doing something like this:
    Code:
    template <int N>
    struct Factorial
    {
        enum { value = N * Factorial<N-1>::value };
    };
    
    template <>
    struct Factorial<1>
    {
        enum { value = 1 };
    };
    
    __declspec(align(Factorial<2>)) struct s {
    	char a;
    	int  b, c;
    Does this compile? I would think that it doesn't, even though the answer for Factorial<2> is generated at compile time.

    Regards,

    Paul McKenzie

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Why doesn't 'sizeof' work here?

    My confusion is because this (which is broadly equivalent) apparently works in gcc:-

    Code:
    __attribute__((__aligned__(sizeof(int)))) struct s {
    	char a;
    	int  b, c;
    };
    So I guess that gcc must be evaluating sizeof at an earlier stage.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Why doesn't 'sizeof' work here?

    Quote Originally Posted by John E View Post
    why doesn't this compile with VC8..?
    Because of syntax violation.

    align (C++)
    Code:
    __declspec( align( # ) ) declarator
    # is the alignment value. Valid entries are integer powers of two from 1 to 8192 (bytes), such as 2, 4, 8, 16, 32, or 64.
    Neither expression nor identifier is expected here.
    Last edited by Igor Vartanov; January 7th, 2013 at 02:09 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured