This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Volatile constants?




Christian Häggström wrote:
> 
> The GCC manual says "An Inline Function is As Fast As a Macro",
> but when I compile this, I got error "initializer element is not constant" on
> 's' initialization.
> 
> inline int abs1(int x) {
>    return x<0 ? -x : x;
> }
> #define abs2(x) (x<0 ? -x : x)
> 
> int r = abs1(-7);
> int s = abs2(-7);

After preprocess

	int s = (-7<0 ? --7 : -7)

I'd guess that the decrement '--7' is what is causing you the problem
This is why you should always use extra parentheses in your #defines

	#define abs(2) ((x)<0 ? -(x) : (x))

Isn't this in a FAQ somewhere?

> 
> Is there any option I have to pass?
> g++ produces a constructor function for 's' but not for 'r'. Why?

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]