Is it possible to use object functions as arguments to theinner_product algorithm?

C

Craig Sanders

Hello all.

I'm hoping that someone might please be able to help me out.

Does anyone know if it is possible to pass object member functions (as
opposed to class member functions) as the 4th and 5th arguments to the
inner_product function?

class
TestClass
{
// Various function declarations omitted for brevity.

int sumFunction(int a, int b)
{
return(a + b);
}

int productFunction(int a, int b)
{
return(a * b);
}
};

int main(void)
{
// Assume vectorA and vectorB are created and populated with
values.

TestClass testClass;


cout << "Result = " <<
inner_product
(
vectorA.begin(),
vectorA.end(),
vectorB.begin(),
testClass.sumFunction,
testClass.productFunction
) <<
endl;

return(0);
}

I can't seem to get main to compile for the case shown above. However,
if sumFunction and productFunction are turned into class functions,
i.e. by prepending their definition with the keyword static, then I
can get main to compile and work. Is anyone able to tell me why this
is the case and how I can get inner_product to work with object
functions?

Thanks in advance.

- Craig
 
M

Michael Doubez

Hello all.

I'm hoping that someone might please be able to help me out.

Does anyone know if it is possible to pass object member functions (as
opposed to class member functions) as the 4th and 5th arguments to the
inner_product function?

class
TestClass
{
    // Various function declarations omitted for brevity.

    int sumFunction(int a, int b)
    {
        return(a + b);
    }

    int productFunction(int a, int b)
    {
        return(a * b);
    }

};

int main(void)
{
    // Assume vectorA and vectorB are created and populated with
values.

    TestClass   testClass;

    cout << "Result = " <<
    inner_product
    (
        vectorA.begin(),
        vectorA.end(),
        vectorB.begin(),
        testClass.sumFunction,
        testClass.productFunction
    ) <<
    endl;

    return(0);

}

I can't seem to get main to compile for the case shown above. However,
if sumFunction and productFunction are turned into class functions,
i.e. by prepending their definition with the keyword static, then I
can get main to compile and work. Is anyone able to tell me why this
is the case

&TestClass::sumFunction and &TestClass::productFunction are member
function pointer an unusable without a TestClass.

Looking at your functions, they should even be free functions.
and how I can get inner_product to work with object
functions?

In the current standard there is no generic bind function to construct
a binary function object (like bind1st for unary).

You can either make your own or use an existing binding library (such
as Boost.Bind).
 
J

James Kanze

I'm hoping that someone might please be able to help me out.
Does anyone know if it is possible to pass object member
functions (as opposed to class member functions) as the 4th
and 5th arguments to the inner_product function?
class
TestClass
{
// Various function declarations omitted for brevity.
int sumFunction(int a, int b)
{
return(a + b);
}
int productFunction(int a, int b)
{
return(a * b);
}
};
int main(void)
{
// Assume vectorA and vectorB are created and populated with values.
TestClass testClass;
cout << "Result = " <<
inner_product
(
vectorA.begin(),
vectorA.end(),
vectorB.begin(),
testClass.sumFunction,
testClass.productFunction
) <<
endl;

return(0);
}
I can't seem to get main to compile for the case shown above.
However, if sumFunction and productFunction are turned into
class functions, i.e. by prepending their definition with the
keyword static, then I can get main to compile and work. Is
anyone able to tell me why this is the case and how I can get
inner_product to work with object functions?

The definition of inner_product is that it does:
acc = binary_op1(acc, binary_op2(*i1, *i2));
where acc is the current accumulator value, i1 and i2 are the
current iterators, and binary_op1 and binary_op2 are the
functions you passed. That expression obviously isn't legal for
non-static member functions.

It's not too clear what you actually want. Given the function
definitions you present, the functions shouldn't even be members
(static or otherwise); they have nothing to do with the class.
And as members, the obviously cannot be used in the above, in
any way, because they require three arguments: the object
they're called on and the two int's. If you actually have a
member function with a single argument (in addition to the
implicit this), and you want to call it on one of the iterators,
something like:
acc = binary_op1(acc, (*i1).binary_op2(*i2));
, then some of the adaptors (mem_fun, etc.) might be able to
help. If, on the other hand, your function actually takes two
arguments (in addition to the implicit this pointer), and uses
members of the class in addition to the two parameters, you'll
have to define a separate helper class, along the lines of:

class Product
{
TestClass* my_owner;
public:
explicit Product(TestClass& owner)
: my_owner(&owner) {}
int operator()(int a, int b) const
{
return my_owner->sumFunction(a, b);
}
};

, and passing an instance of it:
int result
= inner_product(a.begin(), a.end(), b.begin(), 0,
Sum(testClass), Product(testClass));

(Think too about making the functions and the pointer in the
above const.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top