0

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?
Community
  • 1
  • 1
Caroline
  • 920
  • 2
  • 8
  • 25
  • 1
    You can use Clang to build the AST of the existing program, transform that and write out new C++. – Kerrek SB Oct 30 '13 at 20:50
  • 1
    You'll also run into problems with `||`, `&&` and `,` since overloads won't have the sequencing and short-circuiting guarantees of the built-in operators. I'm fairly sure there's no way round these problems without parsing and analysing the code. – Mike Seymour Oct 30 '13 at 20:58
  • You should check [here](http://en.wikipedia.org/wiki/C%2B%2B_operators) to see what you can and cannot overload. The ternary operator cannot be overloaded. – Zach Stark Oct 30 '13 at 21:06
  • Sounds like you should be writing a proper parser for this task. If you want a quick route into writing a simple parser you could use Boost Spirit library - makes writing simple parsers very simple. The library is heavily dependent on using templates, but its the route I'd take. – resigned Oct 31 '13 at 19:57

2 Answers2

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).

resigned
  • 1,044
  • 1
  • 10
  • 11
0

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.

resigned
  • 1,044
  • 1
  • 10
  • 11