Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC's __attribute__((...)) syntax use double parentheses?

Tags:

c

gcc

The documentation for GCC's __attribute__((...)) syntax indicates that attributes must be surrounded by double parentheses, but does not give a rationale for this design decision.

What practical reason would have caused GCC's designers to require this? Does it have anything to do with the preprocessor's handling of double parentheses?

like image 203
Stuart Cook Avatar asked Sep 08 '11 08:09

Stuart Cook


People also ask

What does __ attribute __ mean in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is attribute in embedded C?

Master C and Embedded C Programming- Learn as you go Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.

What are attributes in C programming?

Attributes are a mechanism by which the developer can attach extra information to language entities with a generalized syntax, instead of introducing new syntactic constructs or keywords for each feature.


2 Answers

To make it easier to eliminate it for different compiler. If you have portable code, you have to remove them for other compilers, so you do

#ifndef __GNUC__ #define __attribute__(x) #endif 

The problem is that attributes have various number of arguments and you can combine multiple attributes in one __attribute__ declaration, but C only introduced variadic macros in C99. With double parenthesis the above definition does not need variadic macros.

like image 54
Jan Hudec Avatar answered Sep 21 '22 06:09

Jan Hudec


probably the idea is that you can declare a simple macro that helps to ignore all this in any other C and C++ compiler. If you wouldn't have the second pair of parenthesis that macro would be necessarily one with .... So for compilers that don't support that you would be screwed.

Edit: With this syntax it can simply look like

#ifdef __GNUC__ # define attribute(X) __attribute__(X) #else # define attribute(X) #endif 

and then you would use attribute for your function declarations, e.g.

like image 39
Jens Gustedt Avatar answered Sep 22 '22 06:09

Jens Gustedt