-1

The section of code works. But if I instead use the commented out version,

using StorageType = alignas(alignof(T)) char[sizeof(T)];

I get errors.

template <typename T> struct minipool {
    union minipool_item {
    private:
        //using StorageType = alignas(alignof(T)) char[sizeof(T)];
        using StorageType = char[sizeof(T)];

        // Points to the next freely available item.
        minipool_item *next;
        // Storage of the item. Note that this is a union
        // so it is shared with the pointer "next" above.
        StorageType datum;
        ....
   };
};

What is the correct syntax?

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Ivan
  • 7,448
  • 14
  • 69
  • 134
  • 3
    Possible duplicate of [Where can I use alignas() in C++11?](https://stackoverflow.com/questions/15788947/where-can-i-use-alignas-in-c11) – Swordfish Nov 10 '18 at 03:45

1 Answers1

2

It doesn't work because in C++ there is no mechanism to take an existing type, namely char[sizeof(T)], and create a new type that is identical except for its alignment. If you declare datum to be an array of sizeof(T) chars with the same alignment as T, then the type of datum is still char[sizeof(T)]. The alignment specification can be attached to the member declaration, but not to the type. You can't attach the alignment to the type first, and then use the result of that to declare the member, as you seem to be trying to do.

using StorageType = char[sizeof(T)];
alignas(T) StorageType datum;
Brian Bi
  • 111,498
  • 10
  • 176
  • 312