2

I came across the below use case, but I could not find a proper solution. Is there a way to replace string "<" or ">" with condition < or > in an if condition?

Example:

  string condition = "<";
  if (10 condition 8)   // Here I want to replace condition with <
  {
      // Some code
  }

I don't want to do it like:

if ("<" == condition)
{
   if (10 < 8)
   {
   }
}
else if (">" == condition)
{
   if (10 > 10)
   {
   }
}

And my condition will change during run time. I am just searching for a simple way if exist apart from above.

Use case: The user will give some query like below:

input: 10 > 9   =>  output: true
input: 10 < 7   =>  output: false

Basically I need to parse this query, as I have these 3 words (10, >, 9) as strings, and somehow I want to convert string ">" or "<" to actual symbol > or <.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NDestiny
  • 1,133
  • 1
  • 12
  • 28

5 Answers5

5

You can map the string to a standard library comparator functor such as std::less via a std::map or a std::unordered_map.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Yes, but this involves using the preprocessor, so you couldn't use the value of a string as a dynamically set operator like in the asker's example. This is an interesting strategy to consider, though, as it could make things look a little easier to read. – Robert Columbia Jun 17 '16 at 22:10
  • 3
    The preprocessor does not need to be used for this. – Peter Jun 17 '16 at 22:14
  • 4
    @RobertColumbia: Sorry, I don't understand how you arrive at your conclusion. There's no preprocessor involved. – Cheers and hth. - Alf Jun 17 '16 at 22:14
5

You can't create a new operator in C++ (Can I create a new operator in C++ and how?). I can see where you are coming from with this idea, but the language just doesn't support that. You can, however, create a function that takes two operands and a string "argument" and returns the appropriate value.

bool CustomCompare(int operand1, int operand2, string op)
{
    if (op == "<")
    {
        return operand1<operand2;
    }
    if (op == ">")
    {
        return operand1>operand2;
    }
    if (op == "_")
    {
        return DoTheHokeyPokeyAndTurnTheOperandsAround(operand1, operand2);
    }
}
Community
  • 1
  • 1
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
  • As I told, I have thought about It but looking if there is a another way. just like token pasting operators – NDestiny Jun 17 '16 at 23:26
3
std::function<bool(int,int)> comparator = std::less;
if(comparator(10, 8))
{
    //some code
}

See Also:

melpomene
  • 84,125
  • 8
  • 85
  • 148
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

If you are a C++ Ninja, and you are very stubborn to get it working just the way you wish, there is a way, but it is advanced and complicated.

I mostly write POSIX code for VxWorks, but I guess it can be done for any other.

Let's say you have: string myExpression = "mySize > minSize";

  1. "Rebuild the String as a C code" (save to file, use ccppc, gcc, gpp, whatever toolchain you have.)
  2. You need to link it with your code, at least to get ELF relocations for mySize & minSize (I think it can be done using app-load, if you customize your ld command.
  3. Load the code using ld
  4. Jump to the new address you loaded your code to.

All that said, I would not recommend you to do that:

  1. Very complicated.
  2. Not the most stable, and very bug/error prone.
  3. Can lead to major vulnerabilities "Hacker" style, and proper sanitation is required.

The only pro I can see, is that this method supports everything C has to offer out of the box! BitWise, +-/*^!, even functions as pow(), and such.

A little bit better is:

To compile a function as:

`"bool comparer_AB(int a, int b) { return a " + operator + "}"`

and then call comparer_AB();.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomer W
  • 3,395
  • 2
  • 29
  • 44
0
#include <functional>
#include <map>
#include <string>

int main()
{
  using operation = std::function<bool(int,int)>;
  std::map<std::string, operation> mp = 
    {{"<", std::less<int>()}, 
     {">", std::greater<int>()}};

  int x = 5; 
  int y = 10;
  std::string op = "<";

  bool answer = mp[op](x, y);
}
SCFrench
  • 8,244
  • 2
  • 31
  • 61