1

(Follow-up of Overload ternary ?: operator, or change to if{}else{} in included files.)

Context

Research project here. In my C++ library, I am including C files using #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, for the following cases:

(a >= 0) ? a : -a
(a >= b) ? a : b

Conceptual question

Because I cannot overload the ternary ?: operator in C++, and it only has to work for the above cases, can't I 'overload' them by using macros somewhat like:

#define TO_NEG(a) ((a >= 0) ? a : -a)
#define MAX(a,b)  ((a >= b) ? a : b)

and implement TO_NEG(a) and MAX(a,b)?

Edit: Clarification

With this, I want to achieve that my library

  1. detects ?: in both cases
  2. converts it to TO_NEG or MAX
  3. uses new code I wrote for TO_NEG and MAX
Community
  • 1
  • 1
Caroline
  • 920
  • 2
  • 8
  • 25
  • 5
    The mind boggles at what kind of research project would require this. If you want to design your own language, you could just write a new frontend for LLVM. And if you don't, then it's hard to see why you'd obscure an otherwise fine language in such a barbarous way... – Kerrek SB Nov 18 '13 at 20:07
  • 1
    I don't really understand the question, but if the answer turns out to be "yes, you can use macros to do what you want" then you should take care to properly parenthesize the macro. For example: `#define TO_NEG(a) (((a) >= 0) (a) : -(a))` – Michael Burr Nov 18 '13 at 20:10
  • 2
    But doesn't it work the other way around? You actually want to `#define ((a >= 0) ? a : -a) TO_NEG(a)`, but I don't think it's possible... – Piotr Zierhoffer Nov 18 '13 at 20:11
  • @PiotrZierhoffer Yes, that's what I want to do... if possible. – Caroline Nov 18 '13 at 20:13

3 Answers3

2

Assuming your operator >= produces a MyBool type, I think you can get almost there with:

#define ? .ternary(
#define : )||

template <typename T>
T MyBool::ternary(T val) { 
  if (m_isTrue) return val;
  return T(0)
}

Several things to note:

  1. This doesn't work for a>=b if a is 0 and b is negative.
  2. This is a terrible terrible hack that will break on many edge cases.
  3. You can't actually use : as a symbol in a #define, so you'd have to use some other means of replacing them, at which point, you should probably use something like @raxvan's answer
AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Thank you for the interesting answer. Are you saying it can't be done because of the reason in 3.? – Caroline Nov 18 '13 at 20:44
  • right. You'd have to use a parser other than the c preprocessor to replace ? and : with valid c identifiers first. And it would have to be more sophisticated than a simple search and replace, or you'd scoop up the colons in case labels too. – AShelly Nov 18 '13 at 20:47
  • Follow-up: I was never able to implement this, as explained in this answer. I however, managed to achieve my goal by adjusting another part of the architecture. – Caroline Apr 12 '14 at 17:30
1

You can define something that will replace the ? operator inside the c source file.

The closest thing you can do is the following:

#define QUESTION_OPERATOR(COND,THIS,THAT) ((COND) ? (THIS) : (THAT))
//here you can do custom validations to all the parts if you want

usage:

(cond) ? (This) : (That) //you need to replace this with:
QUESTION_OPERATOR(cond,This,That)
Raxvan
  • 6,257
  • 2
  • 25
  • 46
0

Yes, you can use the ternary operator in macros, but they must be correctly parenthesized:

#define TO_NEG(a) (a >= 0) ? (a) : -(a)
#define MAX(a,b)  (a >= b) ? (a) : (b)
Paul92
  • 8,827
  • 1
  • 23
  • 37
  • 1
    Shouldn't the minus sign go outside the parentheses? `TO_NEG(a + b)` should expand to `-(a + b)`, not to `(-a + b)`. – M Oehm Nov 18 '13 at 20:43