Reputation: 11
Note that this question is about C++ so the following question does not apply to me.
Plus the top answer only consists of links and doesn't really explain anything. GCC attributes, of the form __attribute__((attribute-list)) are an extension in C but exist in C++ mode too. Of course, C++ also has attributes which I refer to as "standard attributes". The two seem to be completely different, so I am asking for a summary of the primary differences and how they interact.
When should I use one over the other?
Is there any point to using __attribute__ in C++?
Upvotes: 1
Views: 651
Reputation: 21878
The reasons for having two sets of attribute annotations are primarily historical. C and C++ lacked methods for expressing useful code annotations like likely
or fallthrough
so each compiler vendor invented his own extensions to support whatever was requested by customers (Visual Studio used __declspec
and GNU used __attribute__
). Recently we also got C++11 attributes to make this even messier.
For C++11-only code you should probly use new standard syntax. For everything else (C or pre-C++11 clients) you better off using traditional portability macro in your library's header:
#ifdef __GNUC__
# define MYLIB_NOINLINE __attribute__((noinline))
#elif defined _MSC_VER
# define MYLIB_NOINLINE __declspec(noinline)
#else
# error Unknown compiler
#endif
Upvotes: 1