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

无人久伴 提交于 2020-06-10 02:14:28

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/40451840/meaning-of-double-bracket-foo-type-name-syntax-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!