C++ parent class alignment

ぐ巨炮叔叔 提交于 2020-01-13 15:00:03

问题


Is it possible to specify alignment of parent class? for example something like (which does not compiled):

template<size_t n>
class Vector : public boost::array<double,n> __attribute__ ((aligned(16)))
{

thanks

well, from comments I gather this is no good way to go. I think I will just stick to composition/alignment of private array


回答1:


We don't need to request alignment on the derived class neither we can. The reason why we don't need is that it is enough to request alignment for the derived class, and that requesting alignment to the derived class will result on layout for the base class that depends on the derived.

class A : public C __attribute__ ((aligned(16)))
{


class B : public C __attribute__ ((aligned(8)))
{

Which will be alignment for C?




回答2:


GCC guarantees that the first base class is at offset zero within the derived class layout. Therefore it's sufficient in this case to align the derived object.

I can't find a good reference at the moment but see here under -wABI, where they describe an exception to the unstated rule: if the base is empty, it may not be at offset zero.

I suppose there would be another exception if the first base lacks a vtable but the derived object has one. array falling into that category, I'd watch out. Of course, the standard leaves layout unspecified: §10/3.



来源:https://stackoverflow.com/questions/2750832/c-parent-class-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!