Skip to main content
edited body
Source Link

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (anyand any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

UPDATE0: the code must compile for both C and C++

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (any any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

UPDATE0: the code must compile for both C and C++

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (and any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

UPDATE0: the code must compile for both C and C++

added 53 characters in body; edited tags
Source Link

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (any any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

UPDATE0: the code must compile for both C and C++

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (any any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (any any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?

UPDATE0: the code must compile for both C and C++

Source Link

single-line #ifdef in C/C++

Consider the following simple C/C++ example:

#define FOO
...
#ifdef FOO
  bar++;
#endif

OK, now I would like to fit that (any any other similar) conditional into one line for the code readability sake (the code has tens of single-line statements that all need to be conditional each depending on different define). Something that, when used, would look like:

#define FOO
...
MY_IFDEF(FOO,bar++;) //Single-line conditional

The goal is to have a reusable macro that can take an arbitrary identifier and, compile the statement if such identifier has been #define-d previously, and do it all in a single line.

Any ideas?