davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (10/18/89)
Trying to get a net program working has brought up an interesting question as to what is a constant expression. Background: large program, defines ASCII control characters with a macro as follows: #define CNTL(c) ('c' & 037) While this work fine with pcc, the ANSI preprocessor doesn't expand it. I know that there is now a way to expand to a string, so I looked for the way to expand to a character. I dind't find it. Therefore, rather than plow through thousands of lines of someone else's code to change all occurences, I redefined the macro as: #define CNTL(c) (#c[0] & 037) This expands as follows: CNTL(M) becomes ("M"[0] & 037). No problem, the string is a constant, the subscript is a constant, the octal value is a constant, and the compiler was happy. Except... where the macro was used to initialize a static value, in which case it was "not a constant expression." What am I missing in the standard? In looking up initializers in the index, section 3.5.7 (L14) uses the term "constant expression." However the index doesn't seem to list that term. I looked in the expressions section and didn't find it. Obviously my assumption that it is an expression made up entirely of elements whose value is known at compile time" is incorrect. Don't waste your time telling me that's a bad way to do the macro, I didn't write it, and I don't defend it. -- bill davidsen (davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen) "The world is filled with fools. They blindly follow their so-called 'reason' in the face of the church and common sense. Any fool can see that the world is flat!" - anon
kremer@cs.odu.edu (Lloyd Kremer) (10/21/89)
In article <1219@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes: > #define CNTL(c) ('c' & 037) >While this work fine with pcc, the ANSI preprocessor doesn't expand it. > > #define CNTL(c) (#c[0] & 037) > > This expands as follows: CNTL(M) becomes ("M"[0] & 037). >where the macro was >used to initialize a static value, in which case it was "not a constant >expression." > >Obviously my assumption that it is an >expression made up entirely of elements whose value is known at compile >time" is incorrect. This seemed like an interesting question, and I've been waiting for a response from one of the real "legal eagles" of the Standard. But since none is forthcoming, I'll throw in my two cents. The macro contains an array subscripting expression, and the compiler apparently fears that the contents of the array may be changed at run time rendering the expression non-constant. However, notice that the array in question is a *string literal*. What is this compiler's policy regarding the writability of string literals? If they are writable, then the compiler's rejection of the expression is justified. If they are not, then the compiler is simply seeing the subscripting operation, and prematurely giving up hope of constancy. Of course, neither possibility offers a solution. The compiler won't accept to expression as constant. I don't suppose it would do any good to put a "const" into the macro. Just hypothesizing. :-) P.S. I support the adoption of the Standard, but it is disheartening to keep seeing its introduction cause working code to break. Growing pains, I guess. -- Lloyd Kremer ...!uunet!xanth!kremer Have terminal...will hack!