Overload ternary ?: operator, or change to if{}else{} in included files

梦想与她 提交于 2019-12-08 09:35:16

问题


Research project here. In my C++ library, I am including C files:

#include "aprogram.c"

which I execute symbolically by overloading (almost) all operators.

I have to be able to detect (condition) ? this : that and extract condition, this and that for usage in my symbolic execution library. However, SO 1, SO 2 and SO 3 amongst others already helped me realise that ?: cannot be overloaded.

  • Is there any way for me to forcibly overload ?: anyways?
  • Can I change all ?: statements in my included C file into ifelse-statements without actually changing the file?

回答1:


According to the C++ standard you are not permitted to overload ?: The best you can do is use C macros (but this can lead to horrible code).




回答2:


Macros were added to the C compiler in the 1970’s to simplify compiler design. Macros are processed by the ‘C Pre-processor’. Unfortunately this pre-processor is naive and does little more than text substitution. The generated code is often unnecessarily complicated, difficult to view (use –E or –P compile option) and hard to debug. Nowadays you should use the compiler to process all of your code (the pre-processor is usually limited to #includes and conditional compilation).

Unfortunately Bjarne Stroustrup decided not to allow you to overload ?: ternary – not for any deep technical reason, but because it was the only tertiary operator and he felt the effort in modifying the compiler was not justified.



来源:https://stackoverflow.com/questions/19693455/overload-ternary-operator-or-change-to-ifelse-in-included-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!