Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a map from enum (class) to std::binary_function

I have this enum (class)

enum class conditional_operator
{
    plus_op,
    or_op,
    not_op
}

And I'd like a std::map that represents these mappings:

std::map<conditional_operator, std::binary_function<bool,bool,bool>> conditional_map = 
        { { conditional_operator::plus_op, std::logical_and<bool> },
          { conditional_operator::or_op,   std::logical_or<bool>},
          { conditional_operator::not_op,  std::binary_negate<bool>} // this seems fishy too, binary_negate is not really what I want :(

Apart from the fact that this doesn't compile:

error: expected primary-expression before '}' token

error: expected primary-expression before '}' token

error: expected primary-expression before '}' token

for each of the three lines, how should I do this? I think a logical_not with a second dummy argument would work, once I get this to compile of course...

EDIT: Could I use lambda's for this?

like image 644
rubenvb Avatar asked Oct 19 '25 01:10

rubenvb


1 Answers

You really want std::function<bool(bool, bool)>, not std::binary_function<bool, bool, bool>. That only exists for typedefs and stuff in C++03. Secondly, I'd just use a lambda- they're short enough and much clearer. The std::logical_and and stuff only exists for C++03 function object creation, and I'd use a lambda over them any day.

std::map<conditional_operator, std::function<bool(bool,bool)>> conditional_map = 
{ 
    { conditional_operator::plus_op, [](bool a, bool b) { return a && b; } },
    { conditional_operator::or_op,   [](bool a, bool b) { return a || b; } },
    { conditional_operator::not_op,  [](bool a, bool b) { return !a; } }
};

Wait- what exact operator are you referring to with not? Because that's unary, as far as I know.

like image 158
Puppy Avatar answered Oct 21 '25 13:10

Puppy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!