When does sizeof require parentheses? I'm wondering becausecompiles in VC++ but not with g++ for me.Code:struct foo {
};
int main()
{
sizeof foo;
}
Printable View
When does sizeof require parentheses? I'm wondering becausecompiles in VC++ but not with g++ for me.Code:struct foo {
};
int main()
{
sizeof foo;
}
Why don't you just use parentheses all the time. It looks better, and makes sizeof look more like a function.
Curiously, a lot of people on this board don't use sizeof(something), they use sizeof something instead, so I'd like to think they are the same, but I'll agree that sizeof(something) looks better.
You need parentheses when you're taking the sizeof a type. You can leave out the parentheses when you're taking the size of a named variable.
In other words:
I'm not sure exactly why this is, but I think that some ambiguities in the grammar might have resulted if "sizeof int" were allowed.Code:int i;
sizeof(i); // valid
sizeof(int); // valid
sizeof i; // valid
sizeof int; // invalid
Personally, I always put parentheses in. Though I never put parentheses in for return statements. (Of course, that's different, because parentheses for return statements are always optional.) I guess it's just a matter of preference.
[edit] As for the original example . . . it doesn't work with g++ because it isn't supposed to. "foo" is a type, so just as you can't use "sizeof int", you can't use "sizeof foo". If you declared a variable of type foo, however, you could take the sizeof that without parentheses.
[/edit]Code:struct foo {
};
int main()
{
foo f;
sizeof f;
}
So is VC++ not conforming to the standard?
Oh it's conforming to the standard, alright, but it does allow a lot of things other compilers bark out.
Calling it smart, or calling bad, call it extensions, or whatever else wish.
VC++ allows some things that aren't standard (but oh do I wish they were).
No, VC++ is not conforming to standard C++ here. As far as I know, "sizeof type" is not allowed by the standard, and so MSVC should forbid it. (Perhaps some compatibility flag needs to be enabled?)
"Conforming" to a standard not only requires allowing that which the standard allows, but also forbidding that which the standard disallows. For example, a C implementation that allows single line (//) comments is not, technically, conforming to C89.
[edit] For example:
If "sizeof type", without parentheses, is allowed, then what is that program to do? [/edit]Code:#include <iostream>
int main() {
struct name {} name;
std::cout << sizeof name << std::endl;
}
Most likely due to language extensions being enabled by default.
More evidence that the parentheses are required, from Wikipedia: http://en.wikipedia.org/wiki/Sizeof
More evidence from Google is ready for the reading: http://www.google.ca/search?hl=en&q=...G=Search&meta=Quote:
sizeof is a compile-time operator that returns the size, in multiples of the size of char, of the variable or parenthesized type-specifier that it precedes.
On the other hand, cppreference.com gets it backwards. :rolleyes: http://www.cppreference.com/keywords/sizeof.html
[edit]Quote:
The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).
Perhaps. Can you enable strict ANSI mode? What happens then? [/edit]Quote:
Most likely due to language extensions being enabled by default.
Shouldn't VC++ at least give a warning for this? I remember doing something that compiled but still gave a "nonstandard extension used" warning. Also, how can I turn off langugage extensions in VC++?
The cppreference.com reference is correct isn't it? Although the example doesn't seem to match.
You can turn them off under project options, C/C++, Language.
It warns about some extensions, but not all sadly.
No, it has it backwards, as I stated.Quote:
The cppreference.com reference is correct isn't it? Although the example doesn't seem to match.
Observe.
This clearly shows that "sizeof i" is valid and "sizeof int" is invalid, which is exactly the opposite of this statement:Code:$ nl -ba sizeofi.cpp
1 #include <cstddef>
2
3 int main() {
4 int i;
5 size_t z;
6 z = sizeof(i); // valid
7 z = sizeof(int); // valid
8 z = sizeof i; // valid
9 z = sizeof int; // invalid
10 }
$ g++ sizeofi.cpp -o sizeofi
sizeofi.cpp: In function ‘int main()’:
sizeofi.cpp:9: error: expected primary-expression before ‘int’
sizeofi.cpp:9: error: expected `;' before ‘int’
$
They are stating that "sizeof int" is valid. And implying that "sizeof i" is not. Oops.Quote:
The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).
I guess I interpreted "variable type" as just "variable". To me, it meant that parentheses are not required with variables, and the example included parentheses around int to emphasize that because int is not a variable, they were required. I thought it would've made more sense if the example used a variable without parentheses instead.
You shall use parentheses... or else you shall be damned to the fire abyss
Section 5.3.3 of the 2003 edition of the C++ Standard states that: "The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id."
Consequently, it is clear that sizeof x is allowed if x is an expression, but sizeof(x) is required if x is a type name.