27

In this article about avoiding False Sharing, the following code snipped for alignment is presented:

// C++ (using C++0x alignment syntax)
template<typename T>
struct cache_line_storage {
   [[ align(CACHE_LINE_SIZE) ]] T data;
   char pad[ CACHE_LINE_SIZE > sizeof(T)
        ? CACHE_LINE_SIZE - sizeof(T)
        : 1 ];
};

What is the meaning of line 4? I've never seen this double bracket syntax before.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
JMC
  • 1,723
  • 1
  • 11
  • 20

1 Answers1

32

That is the attribute specifier syntax. It was introduced as a unified syntax to access what were formerly compiler-specific extensions (now some are standardized).

In this case the code is telling the compiler to align data to CACHE_LINE_SIZE bytes.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Alex Díaz
  • 2,303
  • 15
  • 24