public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* find_if and ptr_fun(isspace<char>) trouble
@ 2004-12-18  9:44 Dan Kegel
  2004-12-18 13:05 ` jlh
  2004-12-19 15:48 ` Gabriel Dos Reis
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Kegel @ 2004-12-18  9:44 UTC (permalink / raw)
  To: gcc-help

I'm trying to port somebody else's code from g++-2.95.3 to g++-3.4.1, but am
running into template problems with STL algorithms and ptr_fun(isspace<char>).

Compiling the following snippet:

#include <cctype>
#include <functional>
#include <algorithm>
#include <deque>
#include <locale>
using namespace std;
int main ()
{
         char     init[7] = {1,2,3,4,5,6,7};
         deque<char> d(init, init+7);
         find_if (d.begin(), d.end(), ptr_fun(isspace<char>));
}

with either gcc-3.2.2 (redhat 9) or gcc-3.4.1 yields the following error:

/usr/include/c++/3.2.2/bits/stl_algo.h: In function `_RandomAccessIter
    std::find_if(_RandomAccessIter, _RandomAccessIter, _Predicate,
    std::random_access_iterator_tag) [with _RandomAccessIter =
    std::_Deque_iterator<char, char&, char*>, _Predicate =
    std::pointer_to_binary_function<char, const std::locale&, bool>]':
/usr/include/c++/3.2.2/bits/stl_algo.h:318:   instantiated from `_InputIter std::find_if(_InputIter, _InputIter, _Predicate) [with _InputIter = std::_Deque_iterator<char, char&, char*>, _Predicate = std::pointer_to_binary_function<char, 
const std::locale&, bool>]'
a.cc:13:   instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:252: no match for call to `(
    std::pointer_to_binary_function<char, const std::locale&, bool>) (char&)'
/usr/include/c++/3.2.2/bits/stl_function.h:465: candidates are: _Result
    std::pointer_to_binary_function<_Arg1, _Arg2, _Result>::operator()(_Arg1,
    _Arg2) const [with _Arg1 = char, _Arg2 = const std::locale&, _Result = bool]

(This is an improvement; the original code didn't specify which
isspace, and blew up in a worse way.)

Can anybody point out what's wrong?

Thanks,
Dan

-- 
Trying to get a job as a c++ developer?  See http://kegel.com/academy/getting-hired.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: find_if and ptr_fun(isspace<char>) trouble
  2004-12-18  9:44 find_if and ptr_fun(isspace<char>) trouble Dan Kegel
@ 2004-12-18 13:05 ` jlh
  2004-12-19 15:39   ` Gabriel Dos Reis
  2004-12-19 15:48 ` Gabriel Dos Reis
  1 sibling, 1 reply; 5+ messages in thread
From: jlh @ 2004-12-18 13:05 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 243 bytes --]


Dan Kegel wrote:
>         find_if (d.begin(), d.end(), ptr_fun(isspace<char>));

AFAIK, isspace isn't a template, so that code doesn't make sense to me.
This seems to work:

t = find_if (d.begin(), d.end(), ptr_fun<int, int>(isspace));

jlh

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: find_if and ptr_fun(isspace<char>) trouble
  2004-12-18 13:05 ` jlh
@ 2004-12-19 15:39   ` Gabriel Dos Reis
  0 siblings, 0 replies; 5+ messages in thread
From: Gabriel Dos Reis @ 2004-12-19 15:39 UTC (permalink / raw)
  To: jlh; +Cc: gcc-help

jlh <jlh@gmx.ch> writes:

| Dan Kegel wrote:
| >         find_if (d.begin(), d.end(), ptr_fun(isspace<char>));
| 
| AFAIK, isspace isn't a template, so that code doesn't make sense to me.

There *is* a function template "isspace" defined in <locale>, it
requires two arguments (as opposed to the C-language isspace defined
in <cctype>); but the code is wrong.

-- Gaby

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: find_if and ptr_fun(isspace<char>) trouble
  2004-12-18  9:44 find_if and ptr_fun(isspace<char>) trouble Dan Kegel
  2004-12-18 13:05 ` jlh
@ 2004-12-19 15:48 ` Gabriel Dos Reis
  2004-12-20  4:59   ` Dan Kegel
  1 sibling, 1 reply; 5+ messages in thread
From: Gabriel Dos Reis @ 2004-12-19 15:48 UTC (permalink / raw)
  To: Dan Kegel; +Cc: gcc-help

Dan Kegel <dank@kegel.com> writes:

| I'm trying to port somebody else's code from g++-2.95.3 to g++-3.4.1, but am
| running into template problems with STL algorithms and ptr_fun(isspace<char>).
| 
| Compiling the following snippet:
| 
| #include <cctype>
| #include <functional>
| #include <algorithm>
| #include <deque>
| #include <locale>
| using namespace std;
| int main ()
| {
|          char     init[7] = {1,2,3,4,5,6,7};
|          deque<char> d(init, init+7);
|          find_if (d.begin(), d.end(), ptr_fun(isspace<char>));

The code is wrong because it pulls in two version of isspace
 
  * one in <cctype> -- the traditional C function;
  * another in <locale> -- the C++ only version that takes two
       arguments: the locale and the character.

and then is trying to use the second version in place of the first.

Try to add

     struct Isspace {
        bool operator()(int c) const { return isspace(c); }
     };

and try

      find_if (d.begin(), d.end(), Isspace());

Also hae a look at:

    http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#4

-- Gaby

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: find_if and ptr_fun(isspace<char>) trouble
  2004-12-19 15:48 ` Gabriel Dos Reis
@ 2004-12-20  4:59   ` Dan Kegel
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Kegel @ 2004-12-20  4:59 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc-help

Gabriel Dos Reis wrote:
> Dan Kegel <dank@kegel.com> writes:
> 
> | I'm trying to port somebody else's code from g++-2.95.3 to g++-3.4.1, but am
> | running into template problems with STL algorithms and ptr_fun(isspace<char>).
> | 
> | Compiling the following snippet:
> | 
> | #include <cctype>
> | #include <functional>
> | #include <algorithm>
> | #include <deque>
> | #include <locale>
> | using namespace std;
> | int main ()
> | {
> |          char     init[7] = {1,2,3,4,5,6,7};
> |          deque<char> d(init, init+7);
> |          find_if (d.begin(), d.end(), ptr_fun(isspace<char>));
> 
> The code is wrong because it pulls in two version of isspace
>  
>   * one in <cctype> -- the traditional C function;
>   * another in <locale> -- the C++ only version that takes two
>        arguments: the locale and the character.
> 
> and then is trying to use the second version in place of the first.

Thanks!
- Dan

-- 
Trying to get a job as a c++ developer?  See http://kegel.com/academy/getting-hired.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-12-20  4:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-18  9:44 find_if and ptr_fun(isspace<char>) trouble Dan Kegel
2004-12-18 13:05 ` jlh
2004-12-19 15:39   ` Gabriel Dos Reis
2004-12-19 15:48 ` Gabriel Dos Reis
2004-12-20  4:59   ` Dan Kegel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).