Arrgh! STL functional templates!

Sorry for the bad title, but I haven't a clue why this won't work.

I've got a template class that is supposed to work like std::logical_and<> (in <functional>) but it takes two predicates to use on the functor arguments:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename BaseType, typename UnaryPredicate1, typename UnaryPredicate2>
struct logical_and_f: std::binary_function <BaseType, BaseType, bool>
  {
  UnaryPredicate1 up1;
  UnaryPredicate2 up2;
  logical_and_f( UnaryPredicate1 up1, UnaryPredicate2 up2 ):
    up1( up1 ), up2( up2 )
    { }
  bool operator () ( const BaseType& a, const BaseType& b )
    {
    return up1( a ) and up2( b );
    }
  };

Now I want to use it to find an C-style octal escape sequence in a string:
1
2
3
4
5
6
7
8
9
10
11
string s = "This is an \\33 escape";
string::iterator i;

i = adjacent_find(
      s.begin(),
      s.end(),
      logical_and_f <char, unary_function <char, bool>, pointer_to_unary_function <int, int> > (
        bind2nd( equal_to <char> (), '\\' ),
        ptr_fun <int, int> ( isdigit )
        )
      );

This should give me an iterator to the \3 in the string.

However, when I compile I get the amazing error:

csv.cpp: In member function

'bool logical_and_f<BaseType, UnaryPredicate1, UnaryPredicate2>::operator()(const BaseType&, const BaseType&)
[with BaseType = char,
UnaryPredicate1 = std::unary_function<char, bool>,
UnaryPredicate2 = std::pointer_to_unary_function<int, int>]':

c:\progra~1\mingw\bin\../lib/gcc/mingw32/4.3.0/include/c++/bits/stl_algo.h:3970:
instantiated from '_FIter std::adjacent_find(_FIter, _FIter, _BinaryPredicate)
[with _FIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
_BinaryPredicate = logical_and_f<char, std::unary_function<char, bool>, std::pointer_to_unary_function<int, int> >]'

csv.cpp:417: instantiated from here
csv.cpp:361: error: no match for call to '(std::unary_function<char, bool>) (const char&)'

where 417 relates to line 10 of the second [code] block (in reference to line 8)
and 361 relates to line 11 of the first [code] block (up1).

I've played around with fixing that (const char&) bit by introducing temporary variables and casting and the like in the template operator function, but it has no apparent effect on the error message.

I have conclusively identified the bind2nd() as the error line (the ptr_fun is fine).

I am obviously doing something really stupid or really wrong, but I can't for the life of me figure out where the types of up1 and bind2nd conflict.

If you want to play with this, you'll need to
1
2
3
4
#include <algorithm>
#include <ccytpe>
#include <functional>
#include <string> 


Help!
If I make one change to the 3rd parameter in the call to adjacent_find I can get this to compile:
change
logical_and_f <char, unary_function <char, bool>, .... )
to
logical_and_f <binder2nd <equal_to<char> >, ...)



Yoinks! The obvious answer...

Thanks a billion! I wouldn't have thought of that. Though now I see why.

Even though binder2nd inherits from unary_function is isn't directly type-compatible because it introduces new template parameters.

Heh. Stupid STL...

:-P
Last edited on
Topic archived. No new replies allowed.