我如何可以模拟alignas(T)? [英] How can I simulate alignas(T)?

查看:171
本文介绍了我如何可以模拟alignas(T)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被用作类型的对象 T 的底层存储阵列:

I have an array which is used as the underlying memory of an object of type T:

char memory[sizeof T];
.
.
.
new(memory) T(whatever);

我怎样才能确保内存正确的 T 目标一致?在C ++ 0x中我只能说:

How can I make sure memory is aligned correctly for T objects? In C++0x I could just say:

alignas(T) char memory[sizeof T];

但Visual Studio 2010中不支持特定的功能呢。

but Visual Studio 2010 does not support that particular feature yet.

推荐答案

通常的(便携式)解决方案是将内存的声明在联盟与任何内置型 T 需要最对齐。 最简单的方法是使用一个联合的所有可能的 考生:

The usual (portable) solution is to put the memory declaration in a union with whatever built-in type in T requires the most alignment. The simplest way would be to use a union with all of the likely candidates:

union MaxAlign
{
    int                 i     ;
    long                l     ;
    long long           ll    ;
    long double         ld    ;
    double              d     ;
    void*               p     ;
    void (*             pf)() ;
    MaxAlign*           ps    ;
} ;

union
{
    MaxAlign dummyForAlignment;
    unsigned char memory[sizeof(T)];
} rawT;

我还没有听说过,更谈不上遭遇,一台机器,上面 没有足够的。一般情况下,只需就足够了。 (这肯定是 足够在Intel和Sparc上。)

I've yet to hear about, much less encounter, a machine where the above didn't suffice. Generally, just double suffices. (It is definitely sufficient on Intel and on Sparc.)

在某些极端情况下,这会导致在分配比多个存储器 必要时,例如如果 T 只包含一个或两个字符。大部分的 当时,这真的不重要,而且不值得担心,但如果 是,则可以使用下列:

In some extreme cases, this can result in allocating more memory than necessary, e.g. if T only contains one or two char. Most of the time, this really doesn't matter, and isn't worth worrying about, but if it is, the following can be used:

namespace MyPrivate {

template< typename T, bool isSmaller >
struct AlignTypeDetail ;

template< typename T >
struct AlignTypeDetail< T, false >
{
    typedef T type ;
} ;

template< typename T >
struct AlignTypeDetail< T, true >
{
    typedef char type ;
} ;

template< typename T, typename U >
struct AlignType
{
    typedef typename AlignTypeDetail< U, (sizeof( T ) < sizeof( U )) >::type
                        type ;
} ;
}

template< typename T >
union MaxAlignFor
{
    typename MyPrivate::AlignType< T, char >::type        c ;
    typename MyPrivate::AlignType< T, short >::type       s ;
    typename MyPrivate::AlignType< T, int >::type         i ;
    typename MyPrivate::AlignType< T, long >::type        l ;
    typename MyPrivate::AlignType< T, long long >::type   ll ;
    typename MyPrivate::AlignType< T, float >::type       f ;
    typename MyPrivate::AlignType< T, double >::type      d ;
    typename MyPrivate::AlignType< T, long double >::type ld ;
    typename MyPrivate::AlignType< T, void* >::type       pc ;
    typename MyPrivate::AlignType< T, MaxAlign* >::type   ps ;
    typename MyPrivate::AlignType< T, void (*)() >::type  pf ;
} ;

在这种情况下, MaxAlignFor&LT; T&GT; 将永远大于 T (并有充分的取向,因为所需的取向将 决不会比规模较大的 T )。

In this case, MaxAlignFor<T> will never be bigger than T (and to have sufficient alignment, since the required alignment will never be larger than the size of T).

请注意,这一切都不是正式的标准保证。但它 将在实际工作中。

Note that none of this is formally guaranteed by the standard. But it will work in practice.

这篇关于我如何可以模拟alignas(T)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆