Boost logo

Boost Users :

From: Jacques A. Vidrine (nectar/boost_at_[hidden])
Date: 2003-05-23 11:24:13


Hello,

I'm new to the Boost library and quite excited to see some of the
tools available to assist writing functional-style code.

Recently I wanted to perform an action on every object in a container
of smart pointers (boost::shared_ptr). I couldn't seem to find a
concise way to do so. Here is a small example (no container involved)
demonstrating the problem I'm encountering.

  #include <iostream>
  #include <string>
  #include <boost/shared_ptr.hpp>
  #include <boost/bind.hpp>
  
  using namespace std;
  using namespace boost;
  
  void
  xprint(ostream *os, shared_ptr<string> p)
  {
        *os << p.get()->c_str() << endl;
  }
  
  void
  xprint2(ostream *os, string *p)
  {
        *os << p->c_str() << endl;
  }
  
  int
  main(int ac, char *av[])
  {
        shared_ptr<string> p(new string("Test"));
  
        bind(xprint, &cerr, _1)(p);
  
  #ifdef THIS_DOES_NOT_WORK
        bind(xprint2, &cerr, _1)(
                bind(&shared_ptr<string>::get, _1)(p)
                );
  #else
        string *q = bind(&shared_ptr<string>::get, _1)(p);
        bind(xprint2, &cerr, _1)(q);
  #endif
  
        return 0;
  }
  

Using GCC 3.2, I encounter a `no match for call' error in the
`THIS_DOES_NOT_WORK' section of code. (Full diagnostics below.) I
have tried many variations using mem_fn, apply, and by specifying the
return type of the functor explicitly, but with no success.

I'm afraid I don't understand the problem. I hoped someone here might
be able to hit me with a clue-stick :-)

Cheers,

-- 
Jacques Vidrine   . NTT/Verio SME      . FreeBSD UNIX       . Heimdal
nectar_at_[hidden] . jvidrine_at_[hidden] . nectar_at_[hidden] . nectar_at_[hidden]
g++ -DTHIS_DOES_NOT_WORK -I/usr/local/include example.cc                                                
example.cc: In function `int main(int, char**)':
example.cc:31: no match for call to `(boost::_bi::bind_t<void, void 
   (*)(std::ostream*, std::string*), 
   boost::_bi::list2<boost::_bi::value<std::ostream*>, boost::arg<1> > >) (
   std::string*)'
/usr/local/include/boost/bind/bind_template.hpp:19: candidates are: 
   boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, 
   L>::operator()() [with R = void, F = void (*)(std::ostream*, std::string*), 
   L = boost::_bi::list2<boost::_bi::value<std::ostream*>, boost::arg<1> >]
/usr/local/include/boost/bind/bind_template.hpp:25:                 
   boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, 
   L>::operator()() const [with R = void, F = void (*)(std::ostream*, 
   std::string*), L = boost::_bi::list2<boost::_bi::value<std::ostream*>, 
   boost::arg<1> >]
/usr/local/include/boost/bind/bind_template.hpp:31:                 
   boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, 
   L>::operator()(A1&) [with A1 = std::string*, R = void, F = void 
   (*)(std::ostream*, std::string*), L = 
   boost::_bi::list2<boost::_bi::value<std::ostream*>, boost::arg<1> >]
/usr/local/include/boost/bind/bind_template.hpp:37:                 
   boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, 
   L>::operator()(A1&) const [with A1 = std::string*, R = void, F = void 
   (*)(std::ostream*, std::string*), L = 
   boost::_bi::list2<boost::_bi::value<std::ostream*>, boost::arg<1> >]

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