This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Compilation errors; bind2nd; ptr_fun


How come the following program gives compilation errors while the one
following it does not? The difference between the two, as shown by the lines
with comments, is that in the former, the second argument to the function
add is passed by reference, while in the latter, a pointer to the second
argument is passed.

#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <map>

int main(void);

void add(string s, map< string, int > & counts)             // difference
{
    counts[s]++;                                            // difference
}

int main(void)
{
    map< string, int > counts;

    for_each
    (
        istream_iterator<string>(cin),
        istream_iterator<string>(),
        bind2nd(ptr_fun(&add), counts)                      // difference
    );

    return 0;
}

#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <map>

int main(void);

void add(string s, map< string, int > * counts)             // difference
{
    (*counts)[s]++;                                         // difference
}

int main(void)
{
    map< string, int > counts;

    for_each
    (
        istream_iterator<string>(cin),
        istream_iterator<string>(),
        bind2nd(ptr_fun(&add), &counts)                     // difference
    );

    return 0;
}

Thanks,
Anjul.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]