Ike Naar
7/10/2011 11:13:00 AM
On 2011-07-10, Dr Nick <3-nospam@temporary-address.org.uk> wrote:
> Ike Naar <ike@sverige.freeshell.org> writes:
>
>> On 2011-07-10, Phil Carmody <thefatphil_demunged@yahoo.co.uk> wrote:
>>> gwowen <gwowen@gmail.com> writes:
>>>> On Jul 8, 12:39?am, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
>>>> wrote:
>>>>
>>>> > However, making it a solitary enum just to avoid making it a #define is
>>>> > nothing more than sophistry.
>>>>
>>>> Scoping is not sophistry.
>>>
>>> You can make #defines disappear just as easily as enums, were that
>>> to be an issue.
>>
>> Actually, it can be a bit tricky to do it well.
>> Can you demonstrate how easily the enums can be replaced by #defines
>> in the program below?
>>
>> #include <assert.h>
>>
>> enum {value = 0};
>> int global = value;
>>
>> void foo(int, int);
>>
>> int main(void)
>> {
>> enum {value = 1};
>> foo(global, value);
>> return 0;
>> }
>>
>> void foo(int i0, int i1)
>> {
>> assert(i0 == value && i1 == 1);
>> }
>
> Sure:
>
> #define GLOBAL_DEFAULT 0
> #define APPROPRIATE_VALUE 1
>
> #include <assert.h>
>
> int global = GLOBAL_DEFAULT;
>
> void foo(int, int);
>
> int main(void)
> {
> foo(global, APPROPRIATE_VALUE);
> return 0;
> }
>
> void foo(int i0, int i1)
> {
> assert(i0 == value && i1 == 1);
> }
>
> A heck of a lot less obfuscated as well!
Names were changed, and a local entity (``value'' inside main) was
moved to the global level. That's more than merely using #defines
instead of enums.
The small example may look obfuscated, but in a large program you
want to be able to use local names (e.g. restricted to a function
body) without running the risk of breaking things globally.
Your solution might break code that uses the name APPROPRIATE_VALUE
for other purposes.