|
Boost Users : |
From: Christian Henning (chhenning_at_[hidden])
Date: 2007-04-16 15:58:14
Hi there, I'm sure it's not a bug in bind but I have found a
surprising behavior when using it. In my project I have to use some
old school c++ classes that use character arrays instead of
std::string. The following code displays a difference when using two
ways of printing those character arrays.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;
class A
{
public:
A( string name )
{
strncpy( _name, name.c_str(), name.length() + 1 );
}
string name() const { return _name; }
private:
char _name[80];
};
void print( const char* s )
{
cout << s << endl;
}
int main(int argc, char* argv[])
{
vector<A> as;
as.push_back( A( "A" ));
as.push_back( A( "B" ));
for_each( as.begin()
, as.end()
, bind( &print
, bind( &std::string::c_str
, bind( &A::name, _1 ))));
vector<A>::const_iterator it = as.begin();
vector<A>::const_iterator end = as.end();
for( ; it != end; ++it )
{
cout << it->name() << endl;
}
return 0;
}
The output should be
A
B
A
B
But instead it's
A
B
The foreach loop doesn't print anything. The problem is when calling
the print function the char* points to an empty string. Can somebody
please enlightening, here.
Thanks,
Christian
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net