Skip to content

define if undefined macro / command #64

@Veslyquix

Description

@Veslyquix
#ifndef UndefinedThing; #define UndefinedThing 12; #endif

The above does not work because #ifndef doesn't support ; at the end of lines.

I was hoping to get this working:

#ifndef NotDef 
    #define NotDef(name) "!(((name) || 1) ?? 0)"
#endif 
#ifndef defr 
    #define defr(name, value) "#if NotDef(name); #define name value; #endif"
#endif 

This way I could do my definitions as such:

defr(EliwoodLord, Mercenary)

instead of:

#ifndef EliwoodLord 
    #define EliwoodLord Mercenary 
#endif 

But it seems that #if does not support ; either. It does not complain about it as strongly as #ifdef, but this does not work when RoyLord is undefined:

#if NotDef(RoyLord); #define RoyLord 3; #endif 
#if NotDef(RoyLord); #define RoyLord 3; #endif 
#if NotDef(RoyLord); #define RoyLord 3; #endif 

However, this identical version on multiple lines does work when RoyLord is undefined.

#if NotDef(RoyLord)
#define RoyLord 3 
#endif 

#if NotDef(RoyLord)
#define RoyLord 3 
#endif 

#if NotDef(RoyLord)
#define RoyLord 3 
#endif 

They both work when RoyLord is defined.

This feels like the sort of macro / feature that could be built in, so I thought I'd mention it here.

Additionally, I got some weird behaviour while I was trying to get this to work:

This works:

#if NotDef(UndefinedThing); #define asdf "12"; #endif 
MESSAGE asdf 

But this does not:

#if NotDef(UndefinedThing); #define asdf 12; MESSAGE asdf; #endif 

And this infinite loops:

#if NotDef(UndefinedThing); #define asdf 12; MESSAGE asdf; #endif 
MESSAGE asdf 

I'm possibly doing something in a way that was not intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions