最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In C++, can I use a preprocessor directive inside of a statement on one line, like I can do in Delphi? - Stack Overflow

programmeradmin3浏览0评论

The C/C++ ternary operator is great:

int x = (someFlag) ? -1 : 1;

Delphi has a nice feature where preprocessor directives can be written on one line. Two examples:

x := {$IFDEF DBG_MY_SYMBOL} -1 {$ELSE} 1 {$ENDIF};
x := {$IF Defined(DBG_MY_SYMBOL)} -1 {$ELSEIF Defined(ANOTHER_SYMBOL)} 1 {$IFEND};

Beautiful!

Does C/C++ have a one-line equivalent like Delphi?

The C/C++ ternary operator is great:

int x = (someFlag) ? -1 : 1;

Delphi has a nice feature where preprocessor directives can be written on one line. Two examples:

x := {$IFDEF DBG_MY_SYMBOL} -1 {$ELSE} 1 {$ENDIF};
x := {$IF Defined(DBG_MY_SYMBOL)} -1 {$ELSEIF Defined(ANOTHER_SYMBOL)} 1 {$IFEND};

Beautiful!

Does C/C++ have a one-line equivalent like Delphi?

Share Improve this question edited Mar 27 at 8:45 phuclv 42.2k15 gold badges184 silver badges527 bronze badges asked Mar 24 at 15:38 AlainDAlainD 6,6356 gold badges66 silver badges118 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 26

Does C/C++ have a one-line equivalent like Delphi?

Unfortunately, no. A C/C++ preprocessor directive takes up a whole line by itself, so there is no direct equivalent in C/C++ to your Delphi example.

However, in C/C++, expressions can span multiple lines, so you can do something like this instead:

x =
  #ifdef DBG_MY_SYMBOL
  -1
  #else
  1
  #endif
;

Or:

x = 
  #if defined(DBG_MY_SYMBOL)
  -1
  #elif defined(ANOTHER_SYMBOL)
  1
  #endif
;

That being said, you should consider using a separate constant for better readability, eg:

#ifdef DBG_MY_SYMBOL
const int MyConstant = -1;
#else
const int MyConstant = 1;
#endif

x = MyConstant;

Or:

#if defined(DBG_MY_SYMBOL)
const int MyConstant = -1;
#elif defined(ANOTHER_SYMBOL)
const int MyConstant = 1;
#endif

x = MyConstant;

Using always-defined macros

You can rely on dead code elimination in this case. Any modern compiler will optimize-out unused part of a branch, which as a constant expression as its condition. If you define the macros to have always a (numeric) value, instead of checking for their definedness, you can use the macro name in the C or C++ expression:

#define CONFIG_FOO 1
/* ⋮ or ⋮ */
#define CONFIG_FOO 0

int calculate_that(void)
{
    int the_foo     = 42;
    int the_not_foo = 24;
    return CONFIG_FOO ? the_foo : the_not_foo;
}

The hacky way

If you depend on use of #ifdef for checking values of your macros, consider using non-macro constants of value zero as the placeholder for disabled options. If you are generating your configuration header, this can be a particularly neat solution. When setting the #defines by hand, it will be cumbersome.

When both variable and the macro are defined, the macro will be used instead of the variable. Therefore, when you disable the option, it will fall back to zero.

Order of these two definitions could seem to be arbitrary, unless you define your macro after the first usage. But you must define the macro after defining the fallback value to not expand the macro name when setting the zero.

bindiff's personal note: Don't do that, pwease!

发布评论

评论列表(0)

  1. 暂无评论