problem with unary_compose

L

Lutz Altmann

hi :)

i'm trying to use the "functional extensions" of the stl.
having a "problem" with the unary_compose..
The following code seems right to me, but doesnt compile -
if i declare "operator()" of "struct one" as const it works - but
i dont understand why it has to be static .. ?!

#include <functional>
#include <iostream>

#include <ext/functional>

using namespace std;
using namespace __gnu_cxx;

struct one: public unary_function<int, int>
{

int operator()(int arg)
{
return ++arg;
}

};

int main(int argc, char** argv) {

one o1,o2;
int i = 2;

unary_compose<one,one> func = compose1(o1,o2);

cout<<func(i)<<endl;

return (EXIT_SUCCESS);
}
 
B

Barry

hi :)

i'm trying to use the "functional extensions" of the stl.
having a "problem" with the unary_compose..
The following code seems right to me, but doesnt compile -
if i declare "operator()" of "struct one" as const it works - but
i dont understand why it has to be static .. ?!

#include <functional>
#include <iostream>

#include <ext/functional>

using namespace std;
using namespace __gnu_cxx;

struct one: public unary_function<int, int>
{

int operator()(int arg)
{
return ++arg;
}

};

int main(int argc, char** argv) {

one o1,o2;
int i = 2;

unary_compose<one,one> func = compose1(o1,o2);

cout<<func(i)<<endl;

return (EXIT_SUCCESS);



}

It's because template class "unary_compose<Op1, Op2>"
has "operator() const", you can see <ext/functional>
for more detail.
Moreover, it's better to add constness to the member function
if it does not modify the members.
And I think unary_compose is so out, check Boost.Bind(tr1)
 

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

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,499
Latest member
DewittK739

Latest Threads

Top