Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: conjunction of binds?

Tags:

c++

bind

boost

Suppose the following two functions:

#include <iostream>
#include <cstdlib> // atoi
#include <cstring> // strcmp
#include <boost/bind.hpp>

bool match1(const char* a, const char* b) {
    return (strcmp(a, b) == 0);
}

bool match2(int a, const char* b) {
    return (atoi(b) == a);
}

Each of these functions takes two arguments, but can be transformed into a callable object that takes only one argument by using (std/boost)bind. Something along the lines of:

boost::bind(match1, "a test");
boost::bind(match2, 42);

I want to be able to obtain, from two functions like these that take one argument and return bool, a callable object that takes two arguments and returns the && of the bools. The type of the arguments is arbitrary.

Something like an operator&& for functions that return bool.

like image 268
Giovanni Funchal Avatar asked Apr 14 '10 15:04

Giovanni Funchal


People also ask

What are the principles of binding theory?

Principle A: An anaphor must be bound in its binding domain. Principle B: A pronoun must be free in its binding domain. Principle C: An R-expressions must be free.

What is binding in English language?

In linguistics, binding is the phenomenon in which anaphoric elements such as pronouns are grammatically associated with their antecedents. For instance in the English sentence "Mary saw herself", the anaphor "herself" is bound by its antecedent "Mary".

What are binding conditions?

Binding Theory determines the interpretation and distribution of pronouns and anaphors. It is formulated in terms of three principles, Condition A, which applies to anaphors, Condition B, which applies to pronouns, and Condition C, which applies to name and other referential expressions (R-expressions).


1 Answers

The return type of boost::bind overloads operator && (as well as many others). So you can write

boost::bind(match1, "a test", _1) && boost::bind(match2, 42, _2);

If you want to store this value, use boost::function. In this case, the type would be

boost::function<bool(const char *, const char *)>

Note that this isn't the return type of boost::bind (that is unspecified), but any functor with the right signature is convertible to a boost::function.

like image 195
Jesse Beder Avatar answered Oct 13 '22 17:10

Jesse Beder