-
January 7th, 2013, 11:23 AM
#1
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
-
January 7th, 2013, 01:06 PM
#2
Re: Why doesn't 'sizeof' work here?
Originally Posted by John E
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
-
January 7th, 2013, 01:28 PM
#3
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
-
January 7th, 2013, 02:07 PM
#4
Re: Why doesn't 'sizeof' work here?
Originally Posted by John E
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|