Home > Archive > VC STL > March 2005 > std::sort and boost::bind
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
std::sort and boost::bind
|
|
| Duane Hebert 2005-03-09, 4:03 pm |
| I have a std::list<std::string> ProcessList
that I need to sort.
I use an inline struct to form the predicate and pass
it to standard sort:
inline struct ProcessSortCriteria {
bool operator()(std::stringtring left,std::string right){ return
SortProcesses(left,right);}
bool SortProcesses(std::string,std::string);
};
std::sort(ProcessList.begin(),ProcessList.end(),ProcessSortCriteria());
This works fine but I'm trying to convert it to use boost::bind. I make the
SortProcesses() function a member and try:
std::sort(ProcessList.begin(),ProcessList.end(),boost::bind(&ProcessBuilder:
:SortProcesses,_1,_2);
I get the following errors:
c:\MSVC\boost_1_31_0\boost\bind.hpp(62): error C2825: 'F::result_type':
cannot form a qualified name
c:\MSVC\boost_1_31_0\boost\bind.hpp(62): error C2039: 'result_type' : is not
a member of 'operator``global namespace'''
This seems to be what the boost help says to do with this. The
SortProcesses()
function is a standard predicate. Am I missing something?
| |
| Pete Becker 2005-03-09, 4:03 pm |
| Duane Hebert wrote:
> inline struct ProcessSortCriteria {
This isn't valid. There's no such thing as "an inline struct." Remove
the "inline." But that has nothing to do with your problem.
>
Am I missing something?
>
You didn't show the code that doesn't work.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
| |
| Duane Hebert 2005-03-09, 4:03 pm |
|
"Pete Becker" <petebecker@acm.org> wrote in message
news:_oGdnUejotQktbLfRVn-qw@giganews.com...
> Duane Hebert wrote:
>
>
> This isn't valid. There's no such thing as "an inline struct." Remove
> the "inline." But that has nothing to do with your problem.
Good point.
>
> You didn't show the code that doesn't work.
It's not that it doesn't work. It doesn't compile
(not exactly the same thing<g> )
I can simplify it. For a predicate I try:
bool SortProcesses(const std::string &left, const std::string &right)
{return true;}
Then I do:
std::sort(ProcessList.begin(),ProcessList.end(),boost::bind(&ProcessBuilder:
:SortProcesses,_1,_2));
I get the same compile errors. SortProcesses() is a member function.
ProcessBuilder is the class.
| |
| Tom Widmer 2005-03-09, 4:03 pm |
| Duane Hebert wrote:
> I can simplify it. For a predicate I try:
>
> bool SortProcesses(const std::string &left, const std::string &right)
> {return true;}
>
> Then I do:
> std::sort(ProcessList.begin(),ProcessList.end(),boost::bind(&ProcessBuilder:
> :SortProcesses,_1,_2));
>
> I get the same compile errors. SortProcesses() is a member function.
> ProcessBuilder is the class.
If you mean a non-static member function, you want:
std::sort(
ProcessList.begin(),
ProcessList.end(),
boost::bind(
&ProcessBuilder::SortProcesses,
PointerToAProcessBuilder,
_1,
_2
)
);
where PointerToAProcessBuilder is what it says (and possibly "this" if
appropriate).
Tom
| |
| Duane Hebert 2005-03-09, 4:03 pm |
|
"Tom Widmer" <tom_usenet@hotmail.com> wrote in message
news:uXfiKvMJFHA.2132@TK2MSFTNGP14.phx.gbl...
> If you mean a non-static member function, you want:
>
> std::sort(
> ProcessList.begin(),
> ProcessList.end(),
> boost::bind(
> &ProcessBuilder::SortProcesses,
> PointerToAProcessBuilder,
> _1,
> _2
> )
> );
>
> where PointerToAProcessBuilder is what it says (and possibly "this" if
> appropriate).
That's the bit that I was missing. Passing it this works. Thanks.
| |
| adebaene@club-internet.fr 2005-03-09, 4:03 pm |
|
Tom Widmer wrote:
> Duane Hebert wrote:
&right)[color=darkred]
std::sort(ProcessList.begin(),ProcessList.end(),boost::bind(&ProcessBuilder:[color=darkred]
function.[color=darkred]
>
> If you mean a non-static member function, you want:
>
> std::sort(
> ProcessList.begin(),
> ProcessList.end(),
> boost::bind(
> &ProcessBuilder::SortProcesses,
> PointerToAProcessBuilder,
> _1,
> _2
> )
> );
>
> where PointerToAProcessBuilder is what it says (and possibly "this"
if
> appropriate).
The other option being to make ProcessBuilder::SortProcesses a static
function, in which case your writing would work.
Arnaud
MVP - VC
| |
| Duane Hebert 2005-03-09, 4:03 pm |
|
<adebaene@club-internet.fr> wrote in message
news:1110390029.397321.128230@l41g2000cwc.googlegroups.com...
>
> The other option being to make ProcessBuilder::SortProcesses a static
> function, in which case your writing would work.
If I make the predicate function static, it will work without using
bind. In this case, I want it to be a const non-static member function.
| |
| Stephen Howe 2005-03-09, 9:00 pm |
| > I have a std::list<std::string> ProcessList
> that I need to sort.
> I use an inline struct to form the predicate and pass
> it to standard sort:
Pardon me but how can this work?
sort() take random iterators but list only has bidrectional iterators.
Something seems odd here as no one else has commented on this.
Stephen Howe
| |
| Duane Hebert 2005-03-09, 9:00 pm |
|
"Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom> wrote in message
news:eN7vmVOJFHA.3428@tk2msftngp13.phx.gbl...
>
> Pardon me but how can this work?
> sort() take random iterators but list only has bidrectional iterators.
> Something seems odd here as no one else has commented on this.
Sorry. It's a vector. I was trying to simplify the example to
get an answer and ...
|
|
|
|
|