문제

Consider the code below:

class Foo
{
    ...
};

template <class T>
class Bar
{
    T t;
};

Is it guaranteed that sizeof(Foo) == sizeof(Bar<Foo>) always evaluates to true across any standard C++ compiler?

Are there any situations in which the above condition would never be true?

도움이 되었습니까?

해결책

I don't believe the standard makes that guarantee - an implementation is free to add padding at the end of Bar.

But it's hard to see why any real implementation would add padding at the end of Bar but not Foo. Unless, say, Foo was explicitly given some non-default alignment (e.g. byte aligned); Bar would then be padded to the default alignment.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top