In C++ is it possible to write a lambda that is a condition i.e. just a condition that evaluates to true or false

nappyfalcon

Example (contrived):

I've written a method that takes can take a "filter" expression, and returns the number of matching items:

int getCount(std::function<bool(int, int)> filter)
{
    // Iterate pairs of numbers p,q
    // Count number of pairs where filter(p, q) is true
    // Return count
}

I know I can invoke this as follows:

getCount([](int x, int y) { return x > y; });

But, since the intention is to write a filter "condition" or "expression" i.e. something very declarative and not imperative, I would ideally like to exclude the "return" statement.

Something along the lines of:

getCount([](int x, int y) { x > y; });

or

getCount([](int x, int y) { x > y });

Obviously the above aren't possible, but is there anything in for example std or boost that would allow me to achieve this intention?

Basically the user of the getCount method needs to be able to provide a filter condition only, without having to say "return" as an imperative statement.

Andrey Semashev

You can use Boost.Phoenix for this:

using namespace boost::phoenix::placeholders;
getCount(_1 > _2);

_1 and _2 are argument placeholders and the whole relational expression forms a function object that returns the result of comparison.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

If condition evaluates to true in debugger/runtime but if block is not executed

From Java

Fail a release pipeline job if custom condition evaluates to false

From Dev

C: False condition is interpreted as true in if statement

From Dev

Weird python literal indexing : ['False', 'True'][condition]

From Dev

Why is `[] == false` is true but just `[]` evaluates to true?

From Dev

How is a local variable created even when IF condition evaluates to false in Ruby?

From Dev

Do not understand why if condition evaluates to True when variable is integer

From Dev

true == false evaluates to true somehow?

From Dev

PHP instanceof returns false for true condition

From Dev

('a' in 'abc' == True) evaluates to False

From Dev

Take elements while a condition evaluates to true (extending ElementArrayFinder)

From Dev

False expression in if condition goes true

From Dev

How can I return true when the condition of where clause is false?

From Dev

angular2 ngIf true or false condition

From Dev

Compiler: What if condition is always true / false

From Dev

if condition with ternary operator not returning true or false

From Dev

Assign True/False condition based on existing columns

From Dev

Nested (Condition) ? true:false statements in JavaScript

From Dev

Weird python literal indexing : ['False', 'True'][condition]

From Dev

How to style only this element if condition is true or false?

From Dev

Check every condition in Python if else even if one evaluates to true

From Dev

awk condition of true and false

From Dev

Or condition never evaluates to true

From Dev

Java: if and else returning true or false at every condition

From Dev

('a' in 'abc' == True) evaluates to False

From Dev

Why does this If condition in VB.NET always evaluates to true and the same code in C# does not

From Dev

Python if condition always evaluates to false regardless of user input

From Dev

I want to assign priceFilter a true/false based on condition in a function

From Dev

"if" condition for boolean settings: == 1, == True or just omit?

Related Related

  1. 1

    If condition evaluates to true in debugger/runtime but if block is not executed

  2. 2

    Fail a release pipeline job if custom condition evaluates to false

  3. 3

    C: False condition is interpreted as true in if statement

  4. 4

    Weird python literal indexing : ['False', 'True'][condition]

  5. 5

    Why is `[] == false` is true but just `[]` evaluates to true?

  6. 6

    How is a local variable created even when IF condition evaluates to false in Ruby?

  7. 7

    Do not understand why if condition evaluates to True when variable is integer

  8. 8

    true == false evaluates to true somehow?

  9. 9

    PHP instanceof returns false for true condition

  10. 10

    ('a' in 'abc' == True) evaluates to False

  11. 11

    Take elements while a condition evaluates to true (extending ElementArrayFinder)

  12. 12

    False expression in if condition goes true

  13. 13

    How can I return true when the condition of where clause is false?

  14. 14

    angular2 ngIf true or false condition

  15. 15

    Compiler: What if condition is always true / false

  16. 16

    if condition with ternary operator not returning true or false

  17. 17

    Assign True/False condition based on existing columns

  18. 18

    Nested (Condition) ? true:false statements in JavaScript

  19. 19

    Weird python literal indexing : ['False', 'True'][condition]

  20. 20

    How to style only this element if condition is true or false?

  21. 21

    Check every condition in Python if else even if one evaluates to true

  22. 22

    awk condition of true and false

  23. 23

    Or condition never evaluates to true

  24. 24

    Java: if and else returning true or false at every condition

  25. 25

    ('a' in 'abc' == True) evaluates to False

  26. 26

    Why does this If condition in VB.NET always evaluates to true and the same code in C# does not

  27. 27

    Python if condition always evaluates to false regardless of user input

  28. 28

    I want to assign priceFilter a true/false based on condition in a function

  29. 29

    "if" condition for boolean settings: == 1, == True or just omit?

HotTag

Archive