Considering :
double data;
double array[10];
std::vector<int> vec(4, 100);
MyClass myclass;
Is there a difference between :
sizeof(double);
sizeof(double[10]);
sizeof(std::vector<int>);
sizeof(MyClass);
and
sizeof(data);
sizeof(array);
sizeof(vec);
sizeof(myclass);
Are the two syntaxes different or strictly equivalent ? Are all of them evaluated at compile-time ? If not, which one is evaluated at run-time ?
The only differences are in syntax and convenience.
Syntactically, you’re allowed to leave out the parentheses in one case, but not the other:
As far as convenience goes, consider something like:
If, for example, you sometime end up changing
afrom afloatto a double, you’d also need to changesizeof(float)tosizeof(double)to match. If you usesizeof(a)throughout, when you changeafrom afloatto adouble, all your uses ofsizeofwill automatically change too, without any editing. The latter is more often a problem in C than C++, such as when calling malloc:vs.
In the first case, changing the first
floattodoublewill produce code that compiles, but has a buffer overrun. In the second case, changing the (only)floattodoubleworks fine.