functionality adapter error

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
class A
{
public:
bool operator()(int x, int y)
{
return x > y;
}
};

int main()
{
int array[5] = { 1, 2, 3, 4, 5 };
//std::remove_copy_if(array, array + 5, std::ostream_iterator<int>(std::cout, " "), std::bind2nd(A(), 15)); //ERROR
std::remove_copy_if(array, array + 5, std::ostream_iterator<int>(std::cout, " "), std::bind2nd(std::greater<int>(), 3));//OK
std::cin.get();
return 0;
}
Legacy C++ function object adapters (like std::bind2nd) require that the function objects they adapt must have certain types defined.

Deriving function objects that take two arguments from binary_function is an easy way to make them compatible with those adaptors. http://en.cppreference.com/w/cpp/utility/functional/binary_function


Legacy C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>

class A : public std::binary_function<int,int,bool>
{
    public:
        bool operator()( int x, int y ) const // *** const added
        {
            return x > y;
        }
};

int main()
{
    int array[5] = { 1, 2, 3, 4, 5 };

    std::remove_copy_if(array, array + 5, std::ostream_iterator<int>(std::cout, " "), std::bind2nd(A(), 3)); // ok now
    std::remove_copy_if( array, array + 5, std::ostream_iterator<int>( std::cout, " " ), std::bind2nd( std::greater<int>(), 3 ) ); //OK

}



std::binary_function<>, std::bind1st() etc. are deprecated and will be removed in C++17.
Shun these; use the generic binder std::bind() instead.
http://en.cppreference.com/w/cpp/utility/functional/bind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>

class A
{
    public:
        bool operator()( int x, int y ) const // *** const added
        {
            return x > y;
        }
};

int main()
{
    int array[5] = { 1, 2, 3, 4, 5 };

    using std::placeholders::_1 ;
    // http://en.cppreference.com/w/cpp/iterator/begin
    std::remove_copy_if( std::begin(array), std::end(array), std::ostream_iterator<int>(std::cout, " "), std::bind( A(), _1, 3 )); // ok now
    std::remove_copy_if( std::begin(array), std::end(array), std::ostream_iterator<int>( std::cout, " " ), std::bind( std::greater<int>(), _1, 3 ) ); //OK

    // http://www.stroustrup.com/C++11FAQ.html#lambda
    std::remove_copy_if( std::begin(array), std::end(array), std::ostream_iterator<int>( std::cout, " " ), []( int x ) { return x > 3 ; } );

    for( int v : array ) // http://www.stroustrup.com/C++11FAQ.html#for
        if( v <= 3 ) std::cout << v << ' ' ;
}


Many Thanks!

I just found, I lost some types definitions(first_argument_type, second_argument_type, result_type).

But I missed the "const" qualifier after "operator()", so the compiler error occured again.

The habit of adding "const" qualifier is so important.
Topic archived. No new replies allowed.