Boost's bind functions

A

Alex Buell

I think one of the program snippets in the Boost's Bind library is
wrong. I've just been trying to find the error in this program as
below, perhaps there's an typo in there somewhere but I can't find it.
Perhaps one of you lot might know what the typo is:

#include <iostream>
#include <string>
#include <boost/bind.hpp>

class tracer
{
public:
tracer()
{
std::cout << "tracer::tracer()\n";
}

tracer(const tracer& other)
{
std::cout << "tracer::tracer(const tracer& other)\n";
}

tracer& operator=(const tracer& other)
{
std::cout << "tracer& tracer::eek:perator=(const tracer&
other)\n"; return *this;
}

~tracer()
{
std::cout << "tracer::~tracer()\n";
}

void print(const std::string& s) const
{
std::cout << s << '\n';
}
};

int main()
{
tracer t;
boost::bind(&tracer::print, t, _1)(std::string("I'm called on a
copy of t\n")); return 0;
}
 
C

Clark S. Cox III

Alex said:
I think one of the program snippets in the Boost's Bind library is
wrong. I've just been trying to find the error in this program as
below, perhaps there's an typo in there somewhere but I can't find it.
Perhaps one of you lot might know what the typo is:

What is the actual problem you're having? Nobody here is a mind-reader
(that I know of)

FWIW, your code compiles fine for me, and outputs:

[dhcp-55:~] ccox% g++ test.cpp -I /usr/local/include/boost && ./a.out
tracer::tracer()
tracer::tracer(const tracer& other)
tracer::tracer(const tracer& other)
tracer::tracer(const tracer& other)
tracer::tracer(const tracer& other)
tracer::tracer(const tracer& other)
tracer::~tracer()
tracer::~tracer()
tracer::tracer(const tracer& other)
tracer::~tracer()
tracer::~tracer()
I'm called on a copy of t

tracer::~tracer()
tracer::~tracer()
tracer::~tracer()
 
M

mlimber

Alex said:
I think one of the program snippets in the Boost's Bind library is
wrong. I've just been trying to find the error in this program as
below, perhaps there's an typo in there somewhere but I can't find it.
Perhaps one of you lot might know what the typo is:

#include <iostream>
#include <string>
#include <boost/bind.hpp>

class tracer
{
public:
tracer()
{
std::cout << "tracer::tracer()\n";
}

tracer(const tracer& other)
{
std::cout << "tracer::tracer(const tracer& other)\n";
}

tracer& operator=(const tracer& other)
{
std::cout << "tracer& tracer::eek:perator=(const tracer&
other)\n"; return *this;
}

~tracer()
{
std::cout << "tracer::~tracer()\n";
}

void print(const std::string& s) const
{
std::cout << s << '\n';
}
};

int main()
{
tracer t;
boost::bind(&tracer::print, t, _1)(std::string("I'm called on a
copy of t\n")); return 0;
}

Looks fine to me. Where do you think there's a typo?

Cheers! --M
 
A

Alex Buell

What is the actual problem you're having? Nobody here is a mind-reader
(that I know of)

Sorry, knew I'd forgotten something. Here's the error (it just won't
compile!):
tracer.cpp: In function ‘int main()’:
tracer.cpp:38: error: no match for call to ‘(boost::_bi::bind_t<void,
boost::_mfi::cmf1<void, tracer, const std::string&>,
boost::_bi::list2<boost::_bi::value<tracer>, boost::arg<1> > >)
(std::string)’ /usr/include/boost/bind/bind_template.hpp:17: note:
candidates are: typename boost::_bi::result_traits<R, F>::type
boost::_bi::bind_t<R, F, L>::eek:perator()() [with R = void, F =
L = said:
] /usr/include/boost/bind/bind_template.hpp:23: note:
typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R,
F, L>::eek:perator()() const [with R = void, F = boost::_mfi::cmf1<void,
tracer, const std::string&>, L =
] /usr/include/boost/bind/bind_template.hpp:29: note:
typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R,
F, L>::eek:perator()(A1&) [with A1 = std::string, R = void, F =
L = said:
] /usr/include/boost/bind/bind_template.hpp:35:
note: typename boost::_bi::result_traits<R, F>::type
boost::_bi::bind_t<R, F, L>::eek:perator()(A1&) const [with A1 =
std::string, R = void, F = boost::_mfi::cmf1<void, tracer, const
std::string&>, L = boost::_bi::list2<boost::_bi::value<tracer>,
boost::arg<1> >]
FWIW, your code compiles fine for me, and outputs:

How strange! I'm using Boost 1.33.1 with G++ 4.1.1 here.
 
A

Alex Buell

Looks fine to me. Where do you think there's a typo?

See my another post with the ouitput from the compiler for details. I'm
totally stumped coz it should compile yet it doesn't! I'm using GCC
4.1.1 with Boost 1.33.1.
 
M

mlimber

Alex said:
See my another post with the ouitput from the compiler for details. I'm
totally stumped coz it should compile yet it doesn't! I'm using GCC
4.1.1 with Boost 1.33.1.

FYI, this compiles fine with Dinkumware's TR1:

#include <iostream>
#include <string>
#include <functional>

class tracer
{
public:
tracer()
{
std::cout << "tracer()\n";
}

tracer(const tracer& other)
{
std::cout << "tracer(const tracer&)\n";
}

tracer& operator=(const tracer& other)
{
std::cout << "operator=\n";
return *this;
}

~tracer()
{
std::cout << "~tracer()\n";
}

void print(const std::string& s) const
{
std::cout << s << '\n';
}

};

int main()
{
tracer t;
std::tr1::bind(&tracer::print, t, std::tr1::placeholders::_1)
( std::string("I'm called on a copy of t\n") );
return 0;
}

You can get rid of the namespace qualifications by adding this to the
top:

using namespace std;
using namespace std::tr1;
using namespace std::tr1::placeholders;

In Boost's version, the placeholders are in an anonymous namespace
rather than std::tr1::placeholders (see
http://boost.org/libs/bind/bind.html#Interface).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top