Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-12-19 13:40:15


Karl Mutch wrote:
> Strangely something that used to work in 1.29 now does not in 1.30.2?
>
> Having switched to Microsoft Visual C++ 7.1 the following code that
> uses member functions fails which used to work on Visual C++ 6.0 SP4
> and 1.29. This is specific to operator () member functions. Named
> member functions
> and also operator methods such as "operator bool ()" or "operator ++
> ()" work. Could be a compiler issue?
>
> class X
> {
> public:
> void operator () () {}
> void operator ++ () {}
>
> void test ()
> {
> // Works
> boost::thread thread_works(boost::bind(&X::operator (), this));
> boost::scoped_ptr<boost::thread> thread_fails(new boost::thread(
> boost::bind(&X::operator ++, this)));
>
> // Fails with
> // error C2059: syntax error : ')'
> // error C2143: syntax error : missing ')' before ';'
> boost::scoped_ptr<boost::thread> thread_fails(new boost::thread(
> boost::bind(&X::operator (), this)));
>
> }
> };

Your code works with VC6 and others, it fails with VC 7.1. Looks like a
parser issue so you should report it to Microsoft.

boost::scoped_ptr<boost::thread> (thread_fails)(new boost::thread(
    boost::bind(&X::operator (), this)));

seems to work, but why would you prefer this over

    boost::thread thread_works(boost::bind(&X::operator (), this));

? And

    boost::thread thread_works3( boost::ref(*this) );

is even simpler. As an aside, this is somehwat fragile, as it's possible for
the X object to be destroyed while the thread is still executing its
operator(); if you can switch to the preferred external thread model:

    boost::thread t( X(...) ); // outside of X's member functions

where the thread itself keeps a copy of the X object you could avoid the
potential issues.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk