0

I want to test the use of alignas(), so I write down these code:

#include <vector>

using namespace std;

template<typename X> 
void user(const vector<X>& vx)
{
    constexpr int bufmax = 1024;
    alignas(X) buffer[bufmax];

    const int max = min(vx.size(), bufmax / sizeof(X));
    uninitialized_copy(vx.begin(), vx.begin()+max, buffer);
}

However, when I compile it with g++, the compiler outputs an error: "expected primary-expression before alignas(X)". Who could explain this? I don't know the exact usages of alignas().

Shangchih Huang
  • 319
  • 3
  • 11

2 Answers2

0

alignas(X) is an aligment specifier, which is applied to type. Your error happens because you didn't specified a type here.

Judging from the rest of the code, you probably want to create aligned buffer of characters: alignas(X) char buffer[bufmax];

Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
0

add below data type to your code

char alignas(X) buffer[bufmax];

alignas(X) char buffer[bufmax];

I also bumped on the same while reading the book.

Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
Victor Mwenda
  • 1,677
  • 17
  • 16