Determining maximum possible alignment in C++

前端 未结 6 681
情歌与酒
情歌与酒 2021-01-31 18:58

Is there any portable way to determine what the maximum possible alignment for any type is?

For example on x86, SSE instructions require 16-byte alignm

6条回答
  •  生来不讨喜
    2021-01-31 19:21

    This is what I'm using. In addition to this, if you're allocating memory then a new()'d array of char with length greater than or equal to max_alignment will be aligned to max_alignment so you can then use indexes into that array to get aligned addresses.

    enum {
                max_alignment = boost::mpl::deref<
                    boost::mpl::max_element<
                            boost::mpl::vector<
                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type,                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type,
                                boost::mpl::int_::value>::type
                            >::type
                        >::type
                    >::type::value
                };
            }
    

提交回复
热议问题