Adrian
Adrian

Reputation: 10911

Why isn't alignas() taking a parameter pack?

According to cppreference.com


alignas specifier (since C++11)

Specifies the alignment requirement of a type or an object.

Syntax

alignas( expression )
alignas( type-id )
alignas( pack ... )

  1. alignas(expression) must be an integral constant expression that evaluates to zero, or to a valid value for an alignment or extended alignment.

  2. Equivalent to alignas(alignof(type))

  3. Equivalent to multiple alignas specifiers applied to the same declaration, one for each member of the parameter pack, which can be either type or non-type parameter pack.


So why don't the following work for g++, clang or VC++?

struct alignas(1, 4) A {};

or

struct alignas(int, double) A {};

Is this a defect?

Edit

Apparently this works for g++ 8.2 and not for clang++ 8.0 or VC++ 19.20.27508.1 so seems to be a defect, and the compilers haven't caught up.

Edit

Apparently, running MinGW's g++ compiler in a cygwin bash shell causes weird behaviour. Works fine when running in a MinGW's bash shell. So, no, this doesn't work under g++ 8.2 either.

Upvotes: 5

Views: 842

Answers (1)

1, 4 or int, double are not parameter packs. This would be an example of a parameter pack used in this context:

template <class... T>
struct Widget
{
  struct alignas(T...) A {};
};

Note that it's possible to apply multiple alignas specifiers to the same declaration, so your examples can be written as alignas(1) alignas(4) and alignas(int) alignas(double) respectively.

Upvotes: 2

Related Questions