Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of double bracket "[[foo()]] type name;" syntax in c++?

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.

like image 657
JMC Avatar asked Nov 06 '16 16:11

JMC


People also ask

What is the meaning of double bracket?

Writing two brackets next to each other means the brackets need to be multiplied together. For example, ( y + 2 ) ( y + 3 ) means ( y + 2 ) × ( y + 3 ) . When expanding double brackets, every term in the first bracket has to be multiplied by every term in the second bracket.

Why do we use double brackets?

It's just a notation for where a variable value should be substituted. They could have picked anything, but double brackets are both reasonably concise yet very unlikely to be found in HTML code otherwise.

What are square brackets used for in C++?

Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.

What are brackets called in C++?

Curly braces (also referred to as just "braces" or as "curly brackets") are a major part of the C++ programming language. They are used in several different constructs, outlined below, and this can sometimes be confusing for beginners.


1 Answers

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.

like image 168
Alejandro Díaz Avatar answered Oct 13 '22 20:10

Alejandro Díaz