Alias for a std::vector that does not take space

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top