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;
};
Printable View
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:
Does this compile? I would think that it doesn't, even though the answer for Factorial<2> is generated at compile time.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;
Regards,
Paul McKenzie
My confusion is because this (which is broadly equivalent) apparently works in gcc:-
So I guess that gcc must be evaluating sizeof at an earlier stage.Code:__attribute__((__aligned__(sizeof(int)))) struct s {
char a;
int b, c;
};
Because of syntax violation.
align (C++)
Code:__declspec( align( # ) ) declarator
Neither expression nor identifier is expected here.Quote:
# 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.