user4358131
user4358131

Reputation: 11

What is the difference between standard attributes and GNU "__attribute__"s?

Note that this question is about C++ so the following question does not apply to me.

__attribute__ in C

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.

Upvotes: 1

Views: 651

Answers (1)

yugr
yugr

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

Related Questions