P
Peter Olcott
Thomas Tutone said:True. But in the example I gave, most compilers would optimize it
away.
You've jumped to an erroneous conclusion. When sizeof is applied to a
reference, it returns the size of the aliased object. Apparently, on
your implementation sizeof(std:vector<int>) is the same as
sizeof(void*). That's just coincidence. Try the following:
#include <iostream>
struct Foo {
double x;
long y;
void* z;
};
int main()
{
using std::cout; using std::endl;
Foo foo;
Foo& bar = foo;
cout << "sizeof foo: " << sizeof(foo) << endl;
cout << "sizeof bar: " << sizeof(bar) << endl;
cout << "sizeof void* " << sizeof(void*) << endl;
}
Please run this program on your system, then report back on the
results. Do you still believe that sizeof a reference must be the same
as sizeof a pointer?
Best regards,
Tom
16, 16, 4 with maximum space optimization turned on.