JMC JMC - 28 days ago 8
C++ Question

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. Sorry if this is a stupid question. I'm very new to c++ and I've failed to google this effectively.

Answer

That is the attribute specifier syntax. It was introduced as a unified syntax to access compiler-specific extensions. In this case the code is telling the compiler to align data to CACHE_LINE_SIZE bytes.

More on atributtes: http://en.cppreference.com/w/cpp/language/attributes

Comments