问题
Is there a way to create user-defined attributes in C++11 or later?
For example,
[[noreturn]] void exit();
is a compiler-defined C++11 attribute.
I'd like to define something like:
[[comingsoon]] int function(int);
Is there a mechanism for this?
Edit: I should mention I'm using Clang.
回答1:
The language provides no way of adding attributes.
Of course, if you are using Clang, you can edit the source of Clang, and add any attributes you like.
回答2:
For now it's not possible to define user-attributes.
There is, as far as i know, no information about if this feature is planned or not. However, look at this FAQ answer from Stroustrup : https://isocpp.org/wiki/faq/cpp11-language-misc#attributes, especially this part :
One planned use for attributes is improved support for OpenMP. For example:
for [ [ omp::parallel() ] ] (int i=0; i<v.size(); ++i) {
// ...
}
It could mean that they plan to allow programmer to define it's own attribute. Wait & see.
来源:https://stackoverflow.com/questions/34818056/user-defined-attributes-in-c11