Skip to content

Commit

Permalink
Create std.traits.classInstanceAlignment
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sh committed Oct 27, 2012
1 parent 6350320 commit 98061d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
37 changes: 37 additions & 0 deletions std/traits.d
Expand Up @@ -2781,6 +2781,43 @@ unittest
}


private template maxAlignment(U...) if(isTypeTuple!U)
{
static if(U.length == 1)
enum maxAlignment = U[0].alignof;
else
enum maxAlignment = max(U[0].alignof, .maxAlignment!(U[1 .. $]));
}


/**
Returns class instance alignment.
Example:
---
class A { byte b; }
class B { long l; }
// As class instancec always has a hidden pointer
static assert(classInstanceAlignment!A == (void*).alignof);
static assert(classInstanceAlignment!B == long.alignof);
---
*/
template classInstanceAlignment(T) if(is(T == class))
{
alias maxAlignment!(void*, typeof(T.tupleof)) classInstanceAlignment;
}

unittest
{
class A { byte b; }
class B { long l; }

static assert(classInstanceAlignment!A == (void*).alignof);
static assert(classInstanceAlignment!B == long.alignof);
}


//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
// Type Conversion
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
Expand Down
10 changes: 1 addition & 9 deletions std/typecons.d
Expand Up @@ -3069,7 +3069,7 @@ unittest
{
// _d_newclass now use default GC alignment (looks like (void*).sizeof * 2 for
// small objects). We will just use the maximum of filed alignments.
alias maxAlignment!(void*, typeof(T.tupleof)) alignment;
alias classInstanceAlignment!T alignment;

static size_t aligned(size_t n)
{
Expand Down Expand Up @@ -3103,14 +3103,6 @@ unittest
return result;
}

private template maxAlignment(U...) if(isTypeTuple!U)
{
static if(U.length == 1)
enum maxAlignment = U[0].alignof;
else
enum maxAlignment = max(U[0].alignof, .maxAlignment!(U[1 .. $]));
}

unittest // Issue 6580 testcase
{
enum alignment = (void*).alignof;
Expand Down

0 comments on commit 98061d2

Please sign in to comment.