On a 32-bit machine I always get the sizeof of a reference 4 bytes even if it’s a reference to a double, so what does it really store in this 4 bytes.
EDIT :
class A{
public:
double& a;
};
int main(){
cout << sizeof(A) << endl; // this will print out 4
}
The standard is pretty clear on
sizeof(C++11, 5.3.3/4):So if you really are taking
sizeof(double&), the compiler is telling you thatsizeof(double)is 4.Update: So, what you really are doing is applying
sizeofto a class type. In that case,So we know that the presence of the reference inside
Acauses it to take up 4 bytes. That’s because even though the standard does not mandate how references are to be implemented, the compiler still has to implement them somehow. This somehow might be very different depending on the context, but for a reference member of a class type the only approach that makes sense is sneaking in adouble*behind your back and calling it adouble&in your face.So if your architecture is 32-bit (in which pointers are 4 bytes long) that would explain the result.
Just keep in mind that the concept of a reference is not tied to any specific implementation. The standard allows the compiler to implement references however it wants.